Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion astrbot/core/platform/sources/kook/kook_event.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import json
from collections.abc import Coroutine
from collections.abc import AsyncGenerator, Coroutine
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -208,3 +208,19 @@ async def send(self, message: MessageChain):
logger.error(f"[kook] {err_msg}")

await super().send(message)

async def send_streaming(
self, generator: AsyncGenerator[MessageChain, None], use_fallback: bool = False
):
"""KOOK 平台不支持流式输出, 调用此方法将回退到普通`send`方法"""
buffer = None
async for chain in generator:
if not buffer:
buffer = chain
Comment on lines +218 to +219
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Use explicit is None check for buffer to avoid relying on MessageChain truthiness.

Since buffer starts as None, using if not buffer: will also trigger if MessageChain is a valid but falsy value (e.g., empty if it implements __bool__/__len__). That changes behavior between None and an empty chain. Use if buffer is None: to distinguish the initial state from an empty MessageChain and make the intent explicit.

else:
buffer.chain.extend(chain.chain)
if not buffer:
return None
buffer.squash_plain()
await self.send(buffer)
return await super().send_streaming(generator, use_fallback)
Comment on lines +225 to +226
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Avoid double-processing and using an already-consumed generator in send_streaming.

Here the generator is fully consumed into buffer, sent once via self.send(buffer), and then passed to super().send_streaming(generator, use_fallback) after it is already exhausted. This makes the superclass call effectively a no-op or fragile if it expects a fresh stream, and risks duplicate or inconsistent output if it also sends content.

Given the docstring that KOOK does not support streaming and this method should fall back to a normal send, consider either:

  • Only aggregating and calling await self.send(buffer) (remove the super().send_streaming call), or
  • Skipping manual aggregation and just return await super().send_streaming(generator, use_fallback) if the base class already provides the fallback.

This avoids using an exhausted generator and prevents double-sending.

Comment on lines +225 to +226
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

在此处调用 super().send_streaming(generator, use_fallback) 是不正确的,因为 generator 已经在前面的 async for 循环中被完全耗尽(exhausted)。父类方法将接收到一个空的生成器,这可能导致预期的逻辑(如指标统计或默认处理)失效。由于你已经通过 await self.send(buffer) 发送了聚合后的消息,且 self.send 内部已经调用了 super().send() 来处理通用的发送逻辑(如指标上报),因此这里不需要再调用父类的流式发送方法。

Suggested change
await self.send(buffer)
return await super().send_streaming(generator, use_fallback)
await self.send(buffer)

Loading