Files
Danny AvilaandGitHub 1ea5528806 fix: preserve image blocks in ensureThinkingBlockInMessages to prevent 174× token amplification (#71)
* fix: message formatting for tool-call sequences

- Introduced a new function, buildToolSequenceContent, to construct structured content arrays from tool-call sequences, preserving image content as native images.
- Updated ensureThinkingBlockInMessages to utilize the new function, ensuring proper handling of structured content and avoiding unnecessary serialization of binary data.
- Removed the deprecated getBufferString function call to streamline message processing.

* docs: add AGENTS.md and CLAUDE.md for project guidelines and documentation

- Created AGENTS.md to provide an overview of the @librechat/agents library, including project structure, code style guidelines, development commands, testing philosophy, and formatting rules.
- Added CLAUDE.md to reference AGENTS.md for comprehensive project instructions and guidelines.

* test: enhance ensureThinkingBlockInMessages tests and add content extraction utility

- Introduced a new helper function, getTextContent, to extract text from message content, improving readability and maintainability of tests.
- Updated existing tests in ensureThinkingBlockInMessages to utilize getTextContent for verifying message content, ensuring consistent handling of both string and structured array formats.
- Added new tests to verify the preservation of image content and prevent unnecessary serialization of large base64 data, addressing token amplification issues.

* chore: update ESLint configuration and refactor role assignment logic

- Added a new ESLint rule, "no-nested-ternary", set to warn, to improve code clarity.
- Refactored role assignment logic in buildToolSequenceContent to enhance readability by replacing nested ternary operators with if-else statements.

* refactor: streamline message content handling in tool sequences

- Introduced new functions, appendMessageContent and flushTextChunks, to improve the organization and readability of message content processing.
- Updated ensureThinkingBlockInMessages to utilize the new functions, ensuring efficient handling of text and image content while preventing unnecessary serialization of binary data.
- Enhanced role assignment logic and content accumulation for better clarity and maintainability.

* refactor: enhance message content handling for image and tool blocks

- Renamed IMAGE_TYPES to IMAGE_BLOCK_TYPES for clarity in identifying block types containing binary image data.
- Introduced isToolMessage function to streamline the identification of tool-role messages.
- Improved appendMessageContent logic to ensure proper handling of image blocks and unrecognized types, preserving context without unnecessary serialization.
- Updated ensureThinkingBlockInMessages to utilize the new isToolMessage function for better readability.

* test: enhance ensureThinkingBlockInMessages tests for content handling

- Updated getTextContent function to accept both string and structured array formats for improved type handling.
- Refined tests to ensure base64 images are preserved as structured blocks and not serialized as text, addressing token amplification issues.
- Added new tests to verify the correct serialization of tool messages and prevent double-serialization of content, ensuring clarity in message processing.

* refactor: improve tool_calls handling in message content processing

- Updated appendMessageContent to ensure tool_calls are only serialized when content does not include tool_use blocks, preventing double serialization.
- Introduced a flag to track the presence of tool_use blocks, enhancing clarity in message processing logic.
- Added fallback logic to handle cases where content arrays are empty but tool_calls are populated, ensuring robust message formatting.

* test: add case for tool_calls serialization with empty content array

- Introduced a new test to verify that tool_calls are correctly serialized when the content array is empty, ensuring robust handling of message formatting.
- Updated existing tests to clarify the serialization behavior of tool_use blocks and prevent double serialization, enhancing overall test coverage for message processing.
2026-03-12 00:11:05 -04:00

99 B

See AGENTS.md for all project instructions, code style, and development guidelines.