-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: prevent crash when reloading conversations with Base64-encoded files #2799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hztBUAA
wants to merge
1
commit into
Chainlit:main
Choose a base branch
from
hztBUAA:fix/base64-file-reload-crash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import json | ||
|
|
||
| import pytest | ||
|
|
||
| from chainlit.data.chainlit_data_layer import ChainlitDataLayer | ||
|
|
||
|
|
||
| class TestConvertElementRowToDict: | ||
| """Test suite for ChainlitDataLayer._convert_element_row_to_dict.""" | ||
|
|
||
| def _make_layer(self): | ||
| return ChainlitDataLayer(database_url="postgresql://fake", storage_client=None) | ||
|
|
||
| def _make_row(self, **overrides): | ||
| row = { | ||
| "id": "elem-1", | ||
| "threadId": "thread-1", | ||
| "stepId": "step-1", | ||
| "metadata": json.dumps({"type": "file"}), | ||
| "url": None, | ||
| "name": "test_file.txt", | ||
| "mime": "text/plain", | ||
| "objectKey": None, | ||
| "chainlitKey": None, | ||
| "display": "inline", | ||
| "size": None, | ||
| "language": None, | ||
| "page": None, | ||
| "autoPlay": None, | ||
| "playerConfig": None, | ||
| "props": "{}", | ||
| } | ||
| row.update(overrides) | ||
| return row | ||
|
|
||
| def test_convert_element_row_with_none_url(self): | ||
| layer = self._make_layer() | ||
| result = layer._convert_element_row_to_dict(self._make_row(url=None)) | ||
| assert result["url"] is None | ||
|
|
||
| def test_convert_element_row_with_none_object_key(self): | ||
| layer = self._make_layer() | ||
| result = layer._convert_element_row_to_dict(self._make_row(objectKey=None)) | ||
| assert result["objectKey"] is None | ||
|
|
||
| def test_convert_element_row_with_valid_url(self): | ||
| layer = self._make_layer() | ||
| result = layer._convert_element_row_to_dict( | ||
| self._make_row(url="https://storage.example.com/file.txt") | ||
| ) | ||
| assert result["url"] == "https://storage.example.com/file.txt" | ||
|
|
||
| def test_convert_element_row_preserves_chainlit_key(self): | ||
| layer = self._make_layer() | ||
| result = layer._convert_element_row_to_dict( | ||
| self._make_row(chainlitKey="file-abc-123") | ||
| ) | ||
| assert result["chainlitKey"] == "file-abc-123" | ||
|
|
||
| def test_convert_element_row_type_from_metadata(self): | ||
| layer = self._make_layer() | ||
| result = layer._convert_element_row_to_dict( | ||
| self._make_row(metadata=json.dumps({"type": "image"})) | ||
| ) | ||
| assert result["type"] == "image" | ||
|
|
||
| def test_convert_element_row_default_type(self): | ||
| layer = self._make_layer() | ||
| result = layer._convert_element_row_to_dict( | ||
| self._make_row(metadata=json.dumps({})) | ||
| ) | ||
| assert result["type"] == "file" | ||
|
|
||
| def test_convert_element_row_full_data(self): | ||
| layer = self._make_layer() | ||
| result = layer._convert_element_row_to_dict( | ||
| self._make_row( | ||
| url="https://storage.example.com/file.txt", | ||
| objectKey="threads/thread-1/files/elem-1", | ||
| chainlitKey="file-abc-123", | ||
| display="side", | ||
| size="large", | ||
| language="python", | ||
| page=3, | ||
| mime="application/pdf", | ||
| props=json.dumps({"custom": "value"}), | ||
| ) | ||
| ) | ||
| assert result["id"] == "elem-1" | ||
| assert result["url"] == "https://storage.example.com/file.txt" | ||
| assert result["objectKey"] == "threads/thread-1/files/elem-1" | ||
| assert result["chainlitKey"] == "file-abc-123" | ||
| assert result["props"] == {"custom": "value"} | ||
|
|
||
|
|
||
| class TestGetElementNoneHandling: | ||
| """Test that get_element does not convert None values to 'None' strings.""" | ||
|
|
||
| def _make_layer(self): | ||
| return ChainlitDataLayer(database_url="postgresql://fake", storage_client=None) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_element_returns_none_url_not_string(self): | ||
| from unittest.mock import AsyncMock | ||
|
|
||
| layer = self._make_layer() | ||
| layer.execute_query = AsyncMock( | ||
| return_value=[ | ||
| { | ||
| "id": "elem-1", | ||
| "threadId": "thread-1", | ||
| "stepId": "step-1", | ||
| "metadata": json.dumps({"type": "file"}), | ||
| "url": None, | ||
| "name": "test.txt", | ||
| "mime": None, | ||
| "objectKey": None, | ||
| "chainlitKey": "ck-1", | ||
| "display": "inline", | ||
| "size": None, | ||
| "language": None, | ||
| "page": None, | ||
| "autoPlay": None, | ||
| "playerConfig": None, | ||
| "props": "{}", | ||
| } | ||
| ] | ||
| ) | ||
| result = await layer.get_element("thread-1", "elem-1") | ||
| assert result is not None | ||
| assert result["url"] is None | ||
| assert result["objectKey"] is None | ||
| assert result["mime"] is None | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_element_not_found(self): | ||
| from unittest.mock import AsyncMock | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This import might well be at the top! Same for other places. |
||
|
|
||
| layer = self._make_layer() | ||
| layer.execute_query = AsyncMock(return_value=[]) | ||
| result = await layer.get_element("thread-1", "nonexistent") | ||
| assert result is None | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I notice changes in two data layers but unit tests only for one, what's the reason? |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Exception handling for resume_thread emits an error but then falls through to the existing "Thread not found" branch, resulting in two error emissions for the same failure.
Prompt for AI agents