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
Fix removing attachments when editing a message
Saving an edited message kept the attachments removed in the edit input,
and kept the previous text when the input was emptied. An empty list of
attachments was dropped at each step: when building the updated message,
when sending it over the websocket, when applying the server message and
when converting it to a shared model message.
  • Loading branch information
burrows99 committed Nov 15, 2026
commit 693acc0675875729a7d8e772d64d795649586e4a
15 changes: 7 additions & 8 deletions packages/jupyter-chat/src/components/messages/message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,13 @@ export function ChatMessageBase(props: ChatMessageProps): JSX.Element {
return;
}

// Update the message
const updatedMessage = { ...message };
if (newMessage.body) {
updatedMessage.body = newMessage.body;
}
if (newMessage.attachments) {
updatedMessage.attachments = newMessage.attachments;
}
// Update the message. The input model omits the attachments when there are
// none, so default to an empty list to drop the ones that were removed.
const updatedMessage = {
...message,
body: newMessage.body ?? '',
attachments: newMessage.attachments ?? []
};
if (newMessage.mentions) {
updatedMessage.mentions = newMessage.mentions;
}
Expand Down
22 changes: 22 additions & 0 deletions packages/jupyterlab-chat/src/__tests__/model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,28 @@ describe('LabChatModel', () => {
expect(model.messages[0].body).toBe('updated');
expect(model.messages[0].edited).toBeUndefined();
});

it('should remove the attachments when updated with an empty list', async () => {
const id = model.sendMessage({
body: 'original',
sender: TEST_USER,
attachments: [{ type: 'file', value: 'notes.md' }]
}) as string;
await flushPromises();
expect(model.messages[0].attachments).toHaveLength(1);

model.updateMessage(id, {
type: 'msg',
id,
body: 'original',
time: 1000,
sender: TEST_USER,
attachments: []
});
await flushPromises();

expect(model.messages[0].attachments).toBeUndefined();
});
});

describe('message attribute updates from shared model', () => {
Expand Down
82 changes: 82 additions & 0 deletions packages/jupyterlab-chat/src/__tests__/websocket-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const REMOTE_USER = {
initials: 'RU'
};

const flushPromises = () => new Promise(resolve => setTimeout(resolve, 0));

function makeWidgetConfig(): IWidgetConfig {
const owner = {} as IWidgetConfig;
owner.config = {};
Expand Down Expand Up @@ -60,6 +62,44 @@ function dispatch(handler: WebSocketHandler, frame: object): void {
(handler as any)._handleMessage({ type: SERVER, ...frame });
}

describe('WebSocketHandler.updateMessage', () => {
const MESSAGE = {
type: 'msg' as const,
id: 'msg-1',
body: 'hello',
time: 1000,
sender: TEST_USER
};
let handler: WebSocketHandler;
let send: jest.Mock;

beforeEach(() => {
handler = makeHandler(makeModel());
send = jest.fn();
(handler as any)._socket = { send, close: jest.fn() };
});

afterEach(() => {
handler.dispose();
});

it('sends an empty attachments list, to remove the attachments', () => {
handler.updateMessage('msg-1', { ...MESSAGE, attachments: [] });

expect(JSON.parse(send.mock.calls[0][0])).toMatchObject({
action: 'edit',
id: 'msg-1',
attachments: []
});
});

it('does not send the attachments when they are not provided', () => {
handler.updateMessage('msg-1', MESSAGE);

expect(JSON.parse(send.mock.calls[0][0])).not.toHaveProperty('attachments');
});
});

describe('WebSocketHandler._handleMessage', () => {
let model: LabChatModel;
let handler: WebSocketHandler;
Expand Down Expand Up @@ -103,6 +143,48 @@ describe('WebSocketHandler._handleMessage', () => {
});
});

describe('message frame', () => {
const ATTACHMENT = { type: 'file', value: 'notes.md' };
const MESSAGE = {
id: 'msg-1',
body: 'hello',
time: 1000,
sender: TEST_USER.username
};

it('removes the attachments of a message updated with an empty list', async () => {
dispatch(handler, {
action: 'message',
message: { ...MESSAGE, attachments: [ATTACHMENT] }
});
await flushPromises();
expect(model.messages[0].attachments).toEqual([ATTACHMENT]);

dispatch(handler, {
action: 'message',
message: { ...MESSAGE, attachments: [] }
});
await flushPromises();

expect(model.messages[0].attachments).toBeUndefined();
});

it('keeps the attachments of a message updated without the attachments', async () => {
dispatch(handler, {
action: 'message',
message: { ...MESSAGE, attachments: [ATTACHMENT] }
});
dispatch(handler, {
action: 'message',
message: { ...MESSAGE, body: 'updated' }
});
await flushPromises();

expect(model.messages[0].body).toBe('updated');
expect(model.messages[0].attachments).toEqual([ATTACHMENT]);
});
});

describe('metadata frame', () => {
it('applies the metadata key to the shared model', () => {
dispatch(handler, {
Expand Down
3 changes: 2 additions & 1 deletion packages/jupyterlab-chat/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,8 @@ export class LabChatModel
if (msg.mime_model) {
ymsg.mime_model = msg.mime_model;
}
if (msg.attachments?.length) {
// Keep an empty list, to remove the attachments when updating a message.
if (msg.attachments) {
ymsg.attachments = msg.attachments.map(att =>
this._sharedModel.setAttachment(att)
);
Expand Down
5 changes: 3 additions & 2 deletions packages/jupyterlab-chat/src/websocket-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ export class WebSocketHandler {
if (message.mime_model) {
msg.mime_model = message.mime_model;
}
if (message.attachments?.length) {
// An empty list is sent too, it removes the attachments of the message.
if (message.attachments) {
msg.attachments = message.attachments;
}
if (message.mentions?.length) {
Expand Down Expand Up @@ -237,7 +238,7 @@ export class WebSocketHandler {
if (msg.mime_model !== undefined) {
content.mime_model = msg.mime_model;
}
if (Array.isArray(msg.attachments) && msg.attachments.length) {
if (Array.isArray(msg.attachments)) {
content.attachments = msg.attachments;
}
if (Array.isArray(msg.mentions) && msg.mentions.length) {
Expand Down
44 changes: 44 additions & 0 deletions ui-tests/tests/attachments.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createChat,
exposeDepsJs,
getPlugin,
hoverFirstMessage,
openChat,
openChatToSide
} from './test-utils';
Expand Down Expand Up @@ -143,6 +144,49 @@ test.describe('#attachments', () => {
);
});

test('Should remove the attachment when editing a message', async ({
page,
tmpPath
}) => {
const chatPanel = await openChat(page, chatPath);
const input = chatPanel.locator('.jp-chat-input-container');

// Attach the markdown file from the temp directory and send the message.
await input.locator('.jp-chat-attach-button').click();
await page.waitForSelector('.jp-Dialog');
const items = page.locator('.jp-Dialog .jp-DirListing-item');
await items.first().locator('.jp-DirListing-itemName').dblclick();
await page.waitForCondition(
async () =>
(await items
.first()
.locator('.jp-DirListing-itemName')
.textContent()) !== tmpPath
);
const item = items.filter({ hasText: 'untitled.md' });
await item.hover();
await item.locator('input[type="checkbox"]').click();
await page.locator('.jp-Dialog button.jp-mod-accept').click();
await input.getByRole('combobox').pressSequentially('Hello');
await input.locator('.jp-chat-send-button').click();

const { message, messageContent } = await hoverFirstMessage(chatPanel);
await expect(message.locator('.jp-chat-attachment')).toHaveCount(1);

// Edit the message, remove its attachment and save.
await message.locator('button[aria-label="Edit"]').click();
const editInput = chatPanel.locator(
'.jp-chat-messages-container .jp-chat-input-container'
);
await editInput.locator('.jp-chat-attachment-remove').click();
await expect(editInput.locator('.jp-chat-attachment')).toHaveCount(0);
await editInput.locator('.jp-chat-save-edit-button').click();

// The message should keep its text and not have attachment anymore.
await expect(messageContent).toHaveText('Hello\n');
await expect(message.locator('.jp-chat-attachment')).toHaveCount(0);
});

test('Should select cells when opening a notebook attachment with cells', async ({
page
}) => {
Expand Down
Loading