chore: small fixes

This commit is contained in:
natoromano
2026-02-23 16:34:59 +01:00
parent bd4b312bba
commit 34a619a54f
4 changed files with 34 additions and 20 deletions
@@ -20,13 +20,13 @@ def fixture_translation_agent_config(settings):
"""Fixture to set used settings for agent configuration."""
settings.TRANSLATION_MAX_CHARS = 100_000
settings.LLM_CONFIGURATIONS = {
settings.LLM_SUMMARIZATION_MODEL_HRID: LLModel(
settings.LLM_DEFAULT_MODEL_HRID: LLModel(
hrid="mistral-model",
model_name="mistral-7b-instruct-v0.1",
human_readable_name="Mistral 7B Instruct",
model_name="mistral-medium-2508",
human_readable_name="Mistral Medium 2508",
profile=None,
provider=LLMProvider(
hrid="mistral",
hrid="mistral-medium-2508",
kind="mistral",
base_url="https://api.mistral.ai/v1",
api_key="testkey",
@@ -71,6 +71,7 @@ async def test_document_translate_single_document(mocked_context, mock_translati
mock_attachment.key = "test_doc.txt"
mock_attachment.file_name = "test_doc.txt"
mock_attachment.content_type = "text/plain"
mock_attachment.size = None
mock_conversation = mock.Mock()
mock_conversation.attachments = _mock_attachments_queryset(mock_attachment)
@@ -103,6 +104,7 @@ async def test_document_translate_uses_last_document(mocked_context, mock_transl
mock_attachment.key = "latest_doc.txt"
mock_attachment.file_name = "latest_doc.txt"
mock_attachment.content_type = "text/plain"
mock_attachment.size = None
mock_conversation = mock.Mock()
mock_conversation.attachments = _mock_attachments_queryset(mock_attachment)
@@ -134,6 +136,7 @@ async def test_document_translate_with_custom_instructions(mocked_context, mock_
mock_attachment.key = "test.txt"
mock_attachment.file_name = "test.txt"
mock_attachment.content_type = "text/plain"
mock_attachment.size = None
mock_conversation = mock.Mock()
mock_conversation.attachments = _mock_attachments_queryset(mock_attachment)
@@ -204,6 +207,7 @@ async def test_document_translate_error_reading_document(mocked_context, mock_tr
mock_attachment.key = "test.txt"
mock_attachment.file_name = "test.txt"
mock_attachment.content_type = "text/plain"
mock_attachment.size = None
mock_conversation = mock.Mock()
mock_conversation.attachments = _mock_attachments_queryset(mock_attachment)
@@ -227,6 +231,7 @@ async def test_document_translate_error_during_translation(mocked_context, mock_
mock_attachment.key = "test.txt"
mock_attachment.file_name = "test.txt"
mock_attachment.content_type = "text/plain"
mock_attachment.size = None
mock_conversation = mock.Mock()
mock_conversation.attachments = _mock_attachments_queryset(mock_attachment)
@@ -259,6 +264,7 @@ async def test_document_translate_too_large(settings, mocked_context, mock_trans
mock_attachment.key = "large_doc.txt"
mock_attachment.file_name = "large_doc.txt"
mock_attachment.content_type = "text/plain"
mock_attachment.size = None
mock_conversation = mock.Mock()
mock_conversation.attachments = _mock_attachments_queryset(mock_attachment)
@@ -286,6 +292,7 @@ async def test_document_translate_empty_result(mocked_context, mock_translation_
mock_attachment.key = "test.txt"
mock_attachment.file_name = "test.txt"
mock_attachment.content_type = "text/plain"
mock_attachment.size = None
mock_conversation = mock.Mock()
mock_conversation.attachments = _mock_attachments_queryset(mock_attachment)
+15 -4
View File
@@ -24,7 +24,7 @@ async def document_translate(
instructions: str | None = None,
) -> ToolReturn:
"""
Translate the full content of uploaded documents into the specified target language.
Translate the full content of the last uploaded document into the specified target language.
Preserve the original markdown formatting unless the instructions say otherwise.
Return this translation directly to the user WITHOUT any modification
or additional summarization.
@@ -76,10 +76,21 @@ async def document_translate(
"You must explain this to the user and ask them to provide documents."
)
doc_name, content = await read_document_content(last_attachment)
# Check content size against the configured limit
# Check file size before reading to avoid loading oversized files into memory.
# Since UTF-8 encodes at least 1 byte per character, a file whose byte size
# already exceeds max_chars cannot possibly be within the character limit.
max_chars = settings.TRANSLATION_MAX_CHARS
if last_attachment.size and last_attachment.size > max_chars:
raise ModelCannotRetry(
"The document is too large to translate. "
"You must explain this to the user, without providing numerical details. "
"Suggest them to reduce the document size by summarizing it or "
"by splitting it into smaller parts. "
"Also offer them to summarize the document in the target language instead, "
"which can be a good alternative to translation for large documents."
)
doc_name, content = await read_document_content(last_attachment)
if len(content) > max_chars:
raise ModelCannotRetry(
@@ -37,7 +37,7 @@
"hrid": "default-provider",
"base_url": "settings.AI_BASE_URL",
"api_key": "settings.AI_API_KEY",
"kind": "openai"
"kind": "mistral"
}
]
}
@@ -29,13 +29,15 @@ export const ToolInvocationItem: React.FC<ToolInvocationItemProps> = ({
?.documents;
const documentIdentifiers: string[] =
Array.isArray(documents) &&
documents.every(
(doc): doc is { identifier: string } =>
typeof doc === 'object' && doc !== null && 'identifier' in doc,
)
documents.every(
(doc): doc is { identifier: string } =>
typeof doc === 'object' && doc !== null && 'identifier' in doc,
)
? documents.map((doc) => doc.identifier)
: [];
const label = documentIdentifiers.length > 1 ? 'Extracting documents...' : 'Extracting document...';
return (
<Box
$direction="row"
@@ -48,13 +50,7 @@ export const ToolInvocationItem: React.FC<ToolInvocationItemProps> = ({
>
<Loader />
<Text $variation="600" $size="md">
{documentIdentifiers.length === 1
? t('Extracting document: {{documents}} ...', {
documents: documentIdentifiers[0],
})
: t('Extracting documents: {{documents}} ...', {
documents: documentIdentifiers.join(', '),
})}
{t(label, { documents: documentIdentifiers.join(', ') })}
</Text>
</Box>
);