ForcePilot/backend/package/yuxi/channel/extensions/wechat_ilink/streaming.py

39 lines
991 B
Python
Raw Normal View History

from __future__ import annotations
import logging
logger = logging.getLogger(__name__)
class ILinkStreaming:
streaming_mode = "block"
ENABLED = True
MIN_CHARS = 200
MAX_CHARS = 1200
COALESCE_MIN_CHARS = 80
COALESCE_MAX_CHARS = 400
COALESCE_IDLE_MS = 500
MAX_BLOCKS = 5
FINISH_MARK = "[↓]"
@classmethod
def create_block_chunker(cls) -> dict:
return {
"mode": "length",
"min_chars": cls.MIN_CHARS,
"max_chars": cls.MAX_CHARS,
"coalesce_min_chars": cls.COALESCE_MIN_CHARS,
"coalesce_max_chars": cls.COALESCE_MAX_CHARS,
"coalesce_idle_ms": cls.COALESCE_IDLE_MS,
}
@classmethod
def should_stream(cls, estimated_length: int) -> bool:
return estimated_length > cls.MIN_CHARS
@classmethod
def build_chunk(cls, text: str, is_final: bool) -> str:
if is_final:
return text
return f"{text}\n{cls.FINISH_MARK}"