Skip to content

Fix removing attachments when editing a message - #587

Open
burrows99 wants to merge 1 commit into
jupyterlab:mainfrom
burrows99:fix/edit-message-clear-attachments
Open

burrows99 wants to merge 1 commit into
jupyterlab:mainfrom
burrows99:fix/edit-message-clear-attachments

Conversation

@burrows99

Copy link
Copy Markdown

Closes #586

Problem

Removing an attachment while editing a message has no effect once the edit is saved:

  1. Send a message with a file attached.
  2. Hover the message, click Edit, remove the attachment with its x button and save.
  3. The message is marked (edited) but the attachment is still there.

The same happens with the text: when a message has an attachment, clearing the text in the edit input and saving keeps the previous text.

I noticed it while editing messages in a local JupyterLab.

The empty list of attachments is lost at each step of the update:

  • ChatMessage only copies attachments (and body) from the edit input when they are truthy, and InputModel.send omits attachments when the list is empty, so the updated message keeps the previous ones.
  • WebSocketHandler.updateMessage does not send an empty attachments list, and _applyMessage ignores an empty list in the message sent back by the server.
  • LabChatModel.toYMessage drops an empty list, so the shared model message keeps its attachments (this is the path used with jupyter_collaboration).

Solution

  • In ChatMessage, the saved message takes the text and the attachments of the edit input as they are, an empty list when all the attachments were removed.
  • In WebSocketHandler and LabChatModel.toYMessage, an empty attachments list is kept instead of being dropped. A message updated without attachments (undefined) still keeps its attachments, so other callers of updateMessage are not affected. The server already handles an empty list in an edit message.
flowchart TD
    A[Message sent with a file attached] --> B{Edit it, remove the file, save}
    B -->|Before| C[File still on the message]
    B -->|After| D[File removed from the message]
Loading

Narrated recording, before then after (sound on):

evidence.mp4

Edge cases and unchanged behavior checked

Checked in the running app (JupyterLab 4.6.6 with the extension built from main at c112d54, then from this branch, without jupyter_collaboration) and shown in the recording:

  • Before: after removing the attachment in the edit input and saving, the file is still on the message.
  • After: the same steps remove the file, and the text is kept.
  • Edge case: with two files attached, removing one keeps the other.
  • Edge case: clearing the text of a message with an attachment leaves a message with only its attachment.
  • Unchanged: editing only the text, saved with the Enter key, keeps the attachment.
  • Unchanged: cancelling an edit after removing the attachment changes nothing.

Checked by tests only:

  • With jupyter_collaboration installed (shared model path): the new UI test, and the new unit test in model.spec.ts.
  • A message updated without attachments keeps its attachments, and nothing is sent for them (websocket-handler.spec.ts).

Tests run

$ jlpm jest src/__tests__/model.spec.ts src/__tests__/websocket-handler.spec.ts   (in packages/jupyterlab-chat)
Test Suites: 2 passed, 2 total
Tests:       25 passed, 25 total

With the source change reverted and the tests kept, 3 of the 5 new unit tests fail (the other 2 cover the behavior that must not change).

$ jlpm playwright test tests/attachments.spec.ts -g "Should remove the attachment|Should add attachments to message"   (in ui-tests)
2 passed

The new UI test Should remove the attachment when editing a message also passes with jupyter_collaboration>=5,<6 installed.

prettier --check and eslint pass on the changed files, and jlpm build (TypeScript) passes.

Not run: the full jest suites, the full UI test suite and the Python tests (no Python change).

AI tools (Claude Code) assisted with this change; the bug was reproduced and the fix verified manually in the running app.

🤖 Generated with Claude Code

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.
@github-actions

Copy link
Copy Markdown
Contributor

Binder 👈 Launch a Binder on branch burrows99/jupyter-chat/fix%2Fedit-message-clear-attachments

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Removing an attachment while editing a message has no effect

1 participant