Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持插件注册消息中间件 #202

Merged
merged 2 commits into from
Sep 18, 2024
Merged

支持插件注册消息中间件 #202

merged 2 commits into from
Sep 18, 2024

Conversation

Soulter
Copy link
Owner

@Soulter Soulter commented Sep 11, 2024

Motivation

中间件支持接受并修改消息事件(AstrMessageEvent),然后将修改后的消息事件传给下一个中间件。最后,消息事件会交由 LLM 模块处理。以此可以将内容审查解耦,并且支持语音转文字(#197)、人格设置等等。

Modifications

在 Context 下新增了 register_middleware() 方法。

一个简单的注册 middleware 的例子如下:

class HelloWorldPlugin:
    def __init__(self, context: Context) -> None:
        self.context = context
        middleware = Middleware(
            name="audio_to_text",
            description="语音转文字",
            origin="HelloWorldPlugin",
            func=self.audio_to_text
        )
        self.context.register_middleware(middleware)

    async def audio_to_text(self, message: AstrMessageEvent, context: Context): # 必须接收这两个参数。
        text = self.audio_to_text(message.message_obj)
        message.message_str = text # 直接原地修改。

@Soulter Soulter merged commit c23e365 into master Sep 18, 2024
4 of 5 checks passed
@Soulter Soulter deleted the feat-middleware branch October 4, 2024 02:33
Soulter added a commit that referenced this pull request Mar 6, 2025
支持插件注册消息中间件
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant