Skip to content

OSSS.ai.utils.content_truncation

OSSS.ai.utils.content_truncation

Smart content truncation utilities for WebSocket events.

This module provides intelligent content truncation that prevents mid-sentence cutoffs while maintaining reasonable event size limits for WebSocket performance.

smart_truncate_content(content, max_length=4000, truncation_indicator='...', preserve_sentences=True, preserve_words=True)

Intelligently truncate content while preserving readability.

This function implements smart truncation that avoids cutting off mid-sentence or mid-word, making WebSocket events more user-friendly while maintaining reasonable size limits for performance.

Parameters

content : str | None The content to potentially truncate, or None max_length : int, default 1000 Maximum length of truncated content (not including truncation indicator) truncation_indicator : str, default "..." String to append when content is truncated preserve_sentences : bool, default True If True, try to truncate at sentence boundaries when possible preserve_words : bool, default True If True, avoid cutting words in half

Returns

str | None Truncated content with optional indicator, or None if input was None

Examples

content = "This is a long sentence that might be truncated." smart_truncate_content(content, max_length=2000) 'This is a long...'

smart_truncate_content(content, max_length=2000, preserve_sentences=True) 'This is a long...'

get_content_truncation_limit(content_type='default')

Get appropriate truncation limits based on content type.

Different types of content may need different truncation limits based on their typical usage patterns and importance.

Parameters

content_type : str Type of content ("refined_question", "critique", "historical_summary", "final_analysis", or "default")

Returns

int Recommended maximum length for this content type

should_truncate_content(content, content_type='default')

Determine if content should be truncated based on length and type.

Parameters

content : str Content to evaluate content_type : str Type of content for context-aware decisions

Returns

bool True if content should be truncated

truncate_for_websocket_event(content, content_type='default')

Truncate content specifically for WebSocket event transmission.

This is the main function used by node wrappers to prepare content for WebSocket events, balancing completeness with performance.

Parameters

content : str Content to prepare for WebSocket transmission content_type : str Type of content for appropriate truncation limits

Returns

str Content ready for WebSocket transmission