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

38 lines
1.1 KiB
Python
Raw Normal View History

from __future__ import annotations
import logging
from yuxi.channel.extensions.googlechat.api import send_message
from yuxi.channel.extensions.googlechat.formatting import chunk_markdown_text
from yuxi.channel.extensions.googlechat.types import ResolvedGoogleChatAccount
logger = logging.getLogger(__name__)
async def stream_text_by_chunks(
account: ResolvedGoogleChatAccount,
space_name: str,
full_text: str,
*,
thread_name: str | None = None,
chunk_limit: int = 4000,
) -> list[str]:
chunks = chunk_markdown_text(full_text, chunk_limit)
message_names: list[str] = []
for chunk in chunks:
result = await send_message(account, space_name, chunk, thread_name=thread_name)
name = result.get("name", "")
if name:
message_names.append(name)
return message_names
async def send_coalesced_text(
account: ResolvedGoogleChatAccount,
space_name: str,
text: str,
*,
thread_name: str | None = None,
) -> str:
result = await send_message(account, space_name, text, thread_name=thread_name)
return result.get("name", "")