Files
MoviePilot/app/agent/callback/__init__.py
2026-03-22 21:07:45 +08:00

42 lines
988 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import threading
from app.log import logger
class StreamingHandler:
"""
流式Token缓冲管理器
负责从 LLM 流式 token 中积累文本,供 Agent 在工具调用之间穿插发送中间消息。
"""
def __init__(self):
self._lock = threading.Lock()
self._buffer = ""
def emit(self, token: str):
"""
接收 LLM 流式 token积累到缓冲区。
"""
with self._lock:
self._buffer += token
def take(self) -> str:
"""
获取当前已积累的消息内容,获取后清空缓冲区。
"""
with self._lock:
if not self._buffer:
return ""
message = self._buffer
logger.info(f"Agent消息: {message}")
self._buffer = ""
return message
def clear(self):
"""
清空缓冲区(不返回内容)
"""
with self._lock:
self._buffer = ""