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
2 changes: 1 addition & 1 deletion site/src/pages/AgentsPage/components/AgentComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function Editor({
onFilePaste={
state.canAttachFiles ? (file) => actions.attachFiles([file]) : undefined
}
acceptFilePasteWhileDisabled={state.isLoading && !state.isReadOnly}
acceptFilePasteWhileDisabled={state.isLoading}
onPaste={actions.resetPromptCycle}
aria-label="Chat message"
aria-describedby={state.warning ? meta.warningId : undefined}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import type { AgentComposerBindings } from "./context";

const workspaceRequiredAttachmentMessage =
"This file type is uploaded into the chat's workspace. Attach a running workspace to the chat, then try again.";
const workspaceUploadPendingSendMessage =
const uploadPendingSendMessage =
"Wait for the current message to finish sending, then add the file again.";

/** Routes files to their upload owner and computes submission readiness. */
export function composerFiles(
bindings: AgentComposerBindings,
resetPromptCycle: () => void,
) {
const { isDisabled, isLoading = false, files } = bindings;
const { isDisabled, isReadOnly, isLoading, files } = bindings;
const { attachments, onAttach, uploadStates, workspaceUploads } = files;
const workspaceUploadEntries = workspaceUploads?.uploads ?? [];

Expand All @@ -40,27 +40,34 @@ export function composerFiles(
});

// Workspace uploads eagerly write bytes into the workspace, so a
// disabled (read-only) composer must not route files to them. The
// same holds while a send is pending: the post-send reset would drop
// the chip after the bytes already landed in the workspace.
const hasWorkspaceAttach = workspaceUploads?.onAttach !== undefined;
const workspaceAttachBlockedBySend = isLoading && hasWorkspaceAttach;
// disabled composer must not route files to them.
let onWorkspaceAttach: ((files: File[]) => void) | undefined;

if (!isDisabled && !isLoading) {
if (!isDisabled) {
onWorkspaceAttach = workspaceUploads?.onAttach;
}

// Splits files between the attachment pipeline and the workspace
// upload path by declared MIME type. Unknown or octet-stream files
// stay on the attachment path where the server classifies bytes.
const attachFiles = (incoming: File[]): boolean => {
if (isReadOnly) {
return false;
}

// The post-send reset clears every chat and workspace file, so files
// added while a send is pending would be dropped after uploading.
if (isLoading) {
toast.error(uploadPendingSendMessage);
return false;
}

const attachable: File[] = [];
const forWorkspace: File[] = [];
const rejected: File[] = [];
// Workspace-routable files that arrived while the workspace
// upload feature is wired but currently unavailable (no
// connected agent, read-only composer, pending send). They need
// connected agent, disabled composer). They need
// a workspace, not a different file type, so they get their own
// toast.
const workspaceRequired: File[] = [];
Expand All @@ -80,15 +87,10 @@ export function composerFiles(
}

if (workspaceRequired.length > 0) {
let message =
toast.error(
workspaceUploads?.unavailableMessage ??
workspaceRequiredAttachmentMessage;

if (workspaceAttachBlockedBySend) {
message = workspaceUploadPendingSendMessage;
}

toast.error(message);
workspaceRequiredAttachmentMessage,
);
}

if (rejected.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export function useComposerRuntime(
canSend,
showSendButton,
showStopButton,
canAttachFiles: bindings.files.onAttach !== undefined,
canAttachFiles: !isReadOnly && bindings.files.onAttach !== undefined,
files: {
attachments: bindings.files.attachments,
uploadStates: bindings.files.uploadStates,
Expand Down
160 changes: 85 additions & 75 deletions site/src/pages/AgentsPage/components/AgentComposers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -582,82 +582,92 @@ describe("ChatComposer", () => {
);
});

it("refuses workspace files while a send is pending", () => {
const onAttach = vi.fn();
const onWorkspaceAttach = vi.fn();
const toastError = vi.spyOn(toast, "error");

renderInput(
<ChatComposer
{...withBindings({
onSend: vi.fn(),
files: {
attachments: [],
onAttach,
workspaceUploads: {
uploads: [],
onAttach: onWorkspaceAttach,
onRemove: vi.fn(),
it.each([
{
name: "dropped files while a send is pending",
isLoading: true,
isReadOnly: false,
paste: false,
message:
"Wait for the current message to finish sending, then add the file again.",
},
{
name: "pasted files while a send is pending",
isLoading: true,
isReadOnly: false,
paste: true,
message:
"Wait for the current message to finish sending, then add the file again.",
},
{
name: "dropped files while read-only",
isLoading: false,
isReadOnly: true,
paste: false,
message: undefined,
},
])(
"refuses $name without losing the draft",
async ({ isLoading, isReadOnly, paste, message }) => {
const inputRef = createRef<ChatMessageInputRef>();
const onAttach = vi.fn();
const onWorkspaceAttach = vi.fn();
const toastError = vi.spyOn(toast, "error").mockClear();

renderInput(
<ChatComposer
{...withBindings({
files: {
attachments: [],
onAttach,
workspaceUploads: {
uploads: [],
onAttach: onWorkspaceAttach,
onRemove: vi.fn(),
},
},
},
isLoading: true,
})}
/>,
);

// The post-send reset would discard the chip after the bytes
// already landed, so the drop is refused with a wait message
// rather than the "attach a workspace" one.
fireEvent.drop(screen.getByRole("textbox", { name: "Chat message" }), {
dataTransfer: {
files: [createMockFile("dataset.zip", "application/zip")],
},
});

expect(onWorkspaceAttach).not.toHaveBeenCalled();
expect(onAttach).not.toHaveBeenCalled();
expect(toastError).toHaveBeenCalledWith(
"Wait for the current message to finish sending, then add the file again.",
);
});

it("refuses pasted workspace files while a send is pending", () => {
const onAttach = vi.fn();
const onWorkspaceAttach = vi.fn();
const toastError = vi.spyOn(toast, "error");

renderInput(
<ChatComposer
{...withBindings({
onSend: vi.fn(),
files: {
attachments: [],
onAttach,
workspaceUploads: {
uploads: [],
onAttach: onWorkspaceAttach,
onRemove: vi.fn(),
},
},
isLoading: true,
})}
/>,
);

fireEvent.paste(screen.getByRole("textbox", { name: "Chat message" }), {
clipboardData: {
files: [createMockFile("dataset.zip", "application/zip")],
types: ["Files"],
getData: () => "",
},
});

expect(onWorkspaceAttach).not.toHaveBeenCalled();
expect(onAttach).not.toHaveBeenCalled();
expect(toastError).toHaveBeenCalledWith(
"Wait for the current message to finish sending, then add the file again.",
);
});
isLoading,
isReadOnly,
inputRef,
initialValue: "Existing draft",
})}
/>,
);

await waitFor(() =>
expect(inputRef.current?.getValue()).toBe("Existing draft"),
);
const textbox = screen.getByRole("textbox", { name: "Chat message" });
const files = [
createMockFile("dataset.zip", "application/zip"),
createMockFile("notes.txt", "text/plain"),
];

if (paste) {
fireEvent.paste(textbox, {
clipboardData: { files, types: ["Files"], getData: () => "" },
});
} else {
fireEvent.drop(textbox, { dataTransfer: { files } });
}

let expectedToasts: string[][] = [];

if (message !== undefined) {
expectedToasts = [[message]];

// Paste attaches clipboard files one at a time.
if (paste) {
expectedToasts = files.map(() => [message]);
}
}

expect(onWorkspaceAttach).not.toHaveBeenCalled();
expect(onAttach).not.toHaveBeenCalled();
expect(toastError.mock.calls).toEqual(expectedToasts);
expect(inputRef.current?.getValue()).toBe("Existing draft");
},
);

it("asks for a workspace when workspace uploads are wired but unavailable", () => {
const onAttach = vi.fn();
Expand Down
Loading