Skip to content
Closed
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
28 changes: 18 additions & 10 deletions site/src/pages/AgentsPage/components/AgentComposerBadges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -360,16 +360,24 @@ const ComposerBadge = ({
badge: ToolBadgeData;
placement: BadgePlacement;
}) => {
const { workspaceSelection, mcp, isViewOnly } = useAgentComposerOptions();
const { workspaceSelection, mcp, isViewOnly, changeWorkspace } =
useAgentComposerOptions();
const { state } = useAgentComposer();
const isDisabled = state.isDisabled;
const inPopover = placement === "popover";
const isRowHidden = placement === "row-hidden";

const onWorkspaceChange = workspaceSelection?.onChange;
let removeWorkspace: (() => void) | undefined;
if (onWorkspaceChange && !isViewOnly) {
removeWorkspace = () => onWorkspaceChange(null);
// Badges keep the workspace X while a change is offered and disable it
// only while workspace selection loads, not while the composer is
// disabled.
const isWorkspaceRemovalOffered =
workspaceSelection?.onChange !== undefined && !isViewOnly;
const canChangeWorkspace = changeWorkspace !== undefined;
const removeWorkspace = () => changeWorkspace?.(null);
let removeLinkedWorkspace: (() => void) | undefined;

if (isWorkspaceRemovalOffered && canChangeWorkspace) {
removeLinkedWorkspace = removeWorkspace;
}

if (badge.kind === "linked-workspace") {
Expand All @@ -387,7 +395,7 @@ const ComposerBadge = ({
chatId={badge.chatId}
sshCommand={badge.sshCommand}
folder={badge.folder}
onRemoveWorkspace={removeWorkspace}
onRemoveWorkspace={removeLinkedWorkspace}
inOverflowPopover={inPopover}
/>
</span>
Expand Down Expand Up @@ -422,11 +430,11 @@ const ComposerBadge = ({
{badge.statusIcon}
<span className="truncate">{badge.name}</span>
</Link>
{removeWorkspace && (
{isWorkspaceRemovalOffered && (
<BadgeDismissButton
onClick={removeWorkspace}
ariaLabel={`Remove workspace ${badge.name}`}
isDisabled={isDisabled}
isDisabled={!canChangeWorkspace}
/>
)}
</span>
Expand All @@ -447,11 +455,11 @@ const ComposerBadge = ({
<span className={badgeCls}>
<MonitorIcon className="size-3" />
<span className="truncate">{badge.name}</span>
{removeWorkspace && (
{isWorkspaceRemovalOffered && (
<BadgeDismissButton
onClick={removeWorkspace}
ariaLabel={`Remove workspace ${badge.name}`}
isDisabled={isDisabled}
isDisabled={!canChangeWorkspace}
/>
)}
</span>
Expand Down
19 changes: 16 additions & 3 deletions site/src/pages/AgentsPage/components/AgentComposerOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@ import { AgentComposerOptionsMenu } from "./AgentComposerOptionsMenu";
const AgentComposerOptionsProvider = ({
children,
...data
}: AgentComposerOptionsData & { children: React.ReactNode }) => (
<OptionsContext value={data}>{children}</OptionsContext>
);
}: AgentComposerOptionsData & { children: React.ReactNode }) => {
const onWorkspaceChange = data.workspaceSelection?.onChange;
const isWorkspaceSelectionLoading =
data.workspaceSelection?.isLoading === true;
let changeWorkspace: ((id: string | null) => void) | undefined;

if (!isWorkspaceSelectionLoading) {
changeWorkspace = onWorkspaceChange;
}

return (
<OptionsContext value={{ ...data, changeWorkspace }}>
{children}
</OptionsContext>
);
};

const AgentComposerOptionsFrame = ({
children,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,17 @@ export type AgentComposerOptionsData = {
};
};

export const OptionsContext = createContext<AgentComposerOptionsData | null>(
/** Options data plus capabilities the provider derives from it. */
type AgentComposerOptionsValue = AgentComposerOptionsData & {
/**
* Present while a workspace change can start: the caller offers one and
* workspace selection is not loading. Composer disablement does not
* withdraw it.
*/
changeWorkspace?: (id: string | null) => void;
};

export const OptionsContext = createContext<AgentComposerOptionsValue | null>(
null,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
export const AgentComposerOptionsMenu = () => {
const options = useAgentComposerOptions();
const composer = useAgentComposer();
const { mcp, workspaceSelection, isViewOnly } = options;
const { mcp, workspaceSelection, isViewOnly, changeWorkspace } = options;
const { state, actions } = composer;
const mcpServers = enabledMcpServers(mcp);

Expand Down Expand Up @@ -73,16 +73,13 @@ export const AgentComposerOptionsMenu = () => {
},
});

const onWorkspaceChange = workspaceSelection?.onChange;
const hasWorkspaceOptions = workspaceSelection?.options !== undefined;
const isWorkspaceChoiceOffered =
hasWorkspaceOptions && onWorkspaceChange !== undefined;
const isWorkspaceSelectionLoading = workspaceSelection?.isLoading === true;
const canChangeWorkspace =
onWorkspaceChange !== undefined && !isWorkspaceSelectionLoading;
hasWorkspaceOptions && workspaceSelection?.onChange !== undefined;
const canChangeWorkspace = changeWorkspace !== undefined;

const selectWorkspace = (id: string | null) => {
onWorkspaceChange?.(id);
changeWorkspace?.(id);
setOpen(false);
};

Expand Down
39 changes: 39 additions & 0 deletions site/src/pages/AgentsPage/components/AgentComposers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import { AppProviders } from "#/App";
import type * as TypesGen from "#/api/typesGenerated";
import { MockMCPServerConfig } from "#/testHelpers/chatEntities";
import { MockWorkspace } from "#/testHelpers/entities";
import { createMockFile } from "#/testHelpers/files";
import { mobileViewportMediaQuery } from "#/utils/mobile";
import type * as speechRecognition from "../hooks/useSpeechRecognition";
Expand Down Expand Up @@ -328,6 +329,44 @@ describe("ChatComposer", () => {
expect(onMCPSelectionChange).not.toHaveBeenCalled();
});

it.each([
{ isDisabled: true, isWorkspaceSelectionLoading: false, removes: true },
{ isDisabled: false, isWorkspaceSelectionLoading: true, removes: false },
])(
"gates workspace removal on workspace selection, not the composer (%o)",
async ({ isDisabled, isWorkspaceSelectionLoading, removes }) => {
const user = userEvent.setup();
const onWorkspaceChange = vi.fn();
renderInput(
<ChatComposer
{...inputProps}
bindings={{ ...inputProps.bindings, isDisabled }}
tools={{
...inputProps.tools,
workspaceSelection: {
options: [MockWorkspace],
selectedId: MockWorkspace.id,
onChange: onWorkspaceChange,
isLoading: isWorkspaceSelectionLoading,
},
}}
/>,
);

await user.click(
screen.getByRole("button", {
name: `Remove workspace ${MockWorkspace.name}`,
}),
);

if (removes) {
expect(onWorkspaceChange).toHaveBeenCalledExactlyOnceWith(null);
} else {
expect(onWorkspaceChange).not.toHaveBeenCalled();
}
},
);

it("swaps Stop for the Queue button once a draft is entered while streaming", async () => {
const user = userEvent.setup();
const inputRef = createRef<ChatMessageInputRef>();
Expand Down
Loading