refactor: consolidate stream smoothing into StreamProcessor with intelligent timing

- Move StreamSmoother functionality directly into StreamProcessor for better integration
- Create ContentExtractor strategy pattern for format-agnostic content extraction
- Implement intelligent dynamic delay calculation based on text length
- Support three text length tiers: short (char-by-char), medium (chunked), long (chunked)
- Remove manual chunk_size and delay_ms configuration - now auto-calculated
- Simplify admin UI to single toggle switch with auto timing adjustment
- Extract format detection logic to reusable content_extractors module
- Improve code maintainability with cleaner architecture
This commit is contained in:
fawney19
2025-12-19 09:46:22 +08:00
parent 85fafeacb8
commit 070121717d
7 changed files with 600 additions and 360 deletions

View File

@@ -34,7 +34,9 @@ from src.api.handlers.base.base_handler import (
from src.api.handlers.base.parsers import get_parser_for_format
from src.api.handlers.base.request_builder import PassthroughRequestBuilder
from src.api.handlers.base.stream_context import StreamContext
from src.api.handlers.base.stream_processor import create_smoothed_stream
from src.api.handlers.base.utils import build_sse_headers
from src.services.system.config import SystemConfigService
# 直接从具体模块导入,避免循环依赖
from src.api.handlers.base.response_parser import (
@@ -352,8 +354,17 @@ class CliMessageHandlerBase(BaseMessageHandler):
# 创建监控流
monitored_stream = self._create_monitored_stream(ctx, stream_generator)
# 创建平滑输出流(如果启用)
smoothing_enabled = bool(
SystemConfigService.get_config(self.db, "stream_smoothing_enabled", False)
)
if smoothing_enabled:
final_stream = create_smoothed_stream(monitored_stream)
else:
final_stream = monitored_stream
return StreamingResponse(
monitored_stream,
final_stream,
media_type="text/event-stream",
headers=build_sse_headers(),
background=background_tasks,