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

feat: Implement baisc system events for application lifecycle #1428

Merged
merged 2 commits into from
Mar 9, 2025

Conversation

lss233
Copy link
Owner

@lss233 lss233 commented Mar 9, 2025

  • Add new event classes for application, plugin, IM, LLM, and workflow events
  • Enhance EventBus with error handling and logging
  • Modify managers and loaders to emit events during key lifecycle stages
  • Refactor PluginEventBus to use global EventBus
  • Update workflow executor to emit execution events

本次提交提供了一些基础事件供插件使用。
插件可以通过下面的方式来注册事件监听器:

from kirara_ai.events.application import ApplicationStopping
from kirara_ai.events.im import IMAdapterStarted, IMAdapterStopped
from kirara_ai.events.listen import listen
from kirara_ai.events import ApplicationStarted
from kirara_ai.events.llm import LLMAdapterLoaded, LLMAdapterUnloaded
from kirara_ai.events.plugin import PluginStarted, PluginStopped
from kirara_ai.plugin_manager.plugin import Plugin

class TestEventBusPlugin(Plugin):
    def on_load(self):
        pass

    def on_start(self):
        self.setup_event_bus()
        pass

    def on_stop(self):
        pass
    
    def setup_event_bus(self):
        @listen(self.event_bus)
        def test_event(event: ApplicationStarted):
            print(event)

        @listen(self.event_bus)
        def test_event(event: ApplicationStopping):
            print(event)

        @listen(self.event_bus)
        def test_event(event: PluginStarted):
            print(event)

        @listen(self.event_bus)
        def test_event(event: PluginStopped):
            print(event)

        @listen(self.event_bus)
        def test_event(event: LLMAdapterLoaded):
            print(event)

        @listen(self.event_bus)
        def test_event(event: LLMAdapterUnloaded):
            print(event)

        @listen(self.event_bus)
        def test_event(event: IMAdapterStarted):
            print(event)

        @listen(self.event_bus)
        def test_event(event: IMAdapterStopped):
            print(event)

好的,这是翻译成中文的 pull request 总结:

Sourcery 总结

实现了一个基本的系统事件机制,用于跟踪应用程序生命周期事件。 这包括添加新的事件类,增强 EventBus 的错误处理能力,修改管理器和加载器以发出事件,重构 PluginEventBus 以使用全局 EventBus,以及更新工作流执行器以发出执行事件。

新功能:

  • 为应用程序、插件、IM、LLM 和工作流事件添加新的事件类,以表示它们生命周期的不同阶段。
  • 添加了应用程序启动和停止、插件加载、启动和停止、IM 适配器启动和停止、LLM 适配器加载和卸载以及工作流执行开始和结束的事件。
  • 引入了一个 listen 装饰器,用于注册事件监听器。

测试:

  • 更新了工作流执行器测试以注入 EventBus 依赖项。
Original summary in English

Summary by Sourcery

Implements a basic system event mechanism for tracking application lifecycle events. This includes adding new event classes, enhancing the EventBus with error handling, modifying managers and loaders to emit events, refactoring PluginEventBus to use the global EventBus, and updating the workflow executor to emit execution events.

New Features:

  • Adds new event classes for application, plugin, IM, LLM, and workflow events to represent different stages of their lifecycles.
  • Adds events for application start and stop, plugin load, start, and stop, IM adapter start and stop, LLM adapter load and unload, and workflow execution begin and end.
  • Introduces a listen decorator for registering event listeners.

Tests:

  • Updates workflow executor tests to inject EventBus dependency.

Copy link
Contributor

sourcery-ai bot commented Mar 9, 2025

## Sourcery 提供的审查者指南

此拉取请求为应用程序引入了一个基本的系统事件机制。它定义了应用程序、插件、IM、LLM 和工作流事件的新事件类。`EventBus` 得到了增强,具有错误处理和日志记录功能。管理器和加载器被修改为在关键生命周期阶段发出事件。`PluginEventBus` 被重构为使用全局 `EventBus`,并且工作流执行器已更新为发出执行事件。

#### 插件加载的序列图

```mermaid
sequenceDiagram
    participant PluginLoader
    participant Plugin
    participant EventBus

    PluginLoader->>Plugin: instantiate_plugin()
    activate Plugin
    PluginLoader->>Plugin: on_load()
    Plugin->>EventBus: post(PluginLoaded(plugin))
    deactivate Plugin

插件启动的序列图

sequenceDiagram
    participant PluginLoader
    participant Plugin
    participant EventBus

    PluginLoader->>Plugin: on_start()
    activate Plugin
    Plugin->>EventBus: post(PluginStarted(plugin))
    deactivate Plugin
Loading

插件停止的序列图

sequenceDiagram
    participant PluginLoader
    participant Plugin
    participant EventBus

    PluginLoader->>Plugin: on_stop()
    activate Plugin
    Plugin->>Plugin: event_bus.unregister_all()
    Plugin->>EventBus: post(PluginStopped(plugin))
    deactivate Plugin
Loading

工作流执行的序列图

sequenceDiagram
    participant WorkflowExecutor
    participant EventBus

    WorkflowExecutor->>EventBus: post(WorkflowExecutionBegin(workflow, self))
    activate WorkflowExecutor
    WorkflowExecutor->>WorkflowExecutor: _execute_nodes()
    WorkflowExecutor->>EventBus: post(WorkflowExecutionEnd(workflow, self, self.results))
    deactivate WorkflowExecutor
Loading

LLM 适配器加载的序列图

sequenceDiagram
    participant LLMManager
    participant LLMBackendAdapter
    participant EventBus

    LLMManager->>LLMBackendAdapter: load_backend()
    activate LLMBackendAdapter
    LLMManager->>EventBus: post(LLMAdapterLoaded(adapter))
    deactivate LLMBackendAdapter
Loading

LLM 适配器卸载的序列图

sequenceDiagram
    participant LLMManager
    participant LLMBackendAdapter
    participant EventBus

    LLMManager->>LLMBackendAdapter: unload_backend()
    activate LLMBackendAdapter
    LLMManager->>EventBus: post(LLMAdapterUnloaded(adapter))
    deactivate LLMBackendAdapter
Loading

IM 适配器启动的序列图

sequenceDiagram
    participant IMManager
    participant IMAdapter
    participant EventBus

    IMManager->>IMAdapter: start()
    activate IMAdapter
    IMManager->>EventBus: post(IMAdapterStarted(adapter))
    deactivate IMAdapter
Loading

IM 适配器停止的序列图

sequenceDiagram
    participant IMManager
    participant IMAdapter
    participant EventBus

    IMManager->>IMAdapter: stop()
    activate IMAdapter
    IMManager->>EventBus: post(IMAdapterStopped(adapter))
    deactivate IMAdapter
Loading

应用程序启动的序列图

sequenceDiagram
    participant Application
    participant EventBus

    Application->>EventBus: post(ApplicationStarted())
Loading

应用程序停止的序列图

sequenceDiagram
    participant Application
    participant EventBus

    Application->>EventBus: post(ApplicationStopping())
Loading

PluginEventBus 的更新类图

classDiagram
    class PluginEventBus {
        -event_bus: EventBus
        -registered_listeners: List[Callable]
        +__init__(event_bus: EventBus)
        +register(event_type: Type, listener: Callable)
        +unregister(event_type: Type, listener: Callable)
        +post(event)
        +unregister_all()
    }
    PluginEventBus -- EventBus : uses
Loading

文件级别更改

变更 详情 文件
引入了各种应用程序组件的新事件类。
  • 添加了 ApplicationStartedApplicationStopping 事件。
  • 添加了 PluginLoadedPluginStartedPluginStopped 事件。
  • 添加了 IMAdapterStartedIMAdapterStopped 事件。
  • 添加了 LLMAdapterLoadedLLMAdapterUnloaded 事件。
  • 添加了 WorkflowExecutionBeginWorkflowExecutionEnd 事件。
kirara_ai/events/__init__.py
kirara_ai/events/application.py
kirara_ai/events/workflow.py
kirara_ai/events/plugin.py
kirara_ai/events/llm.py
kirara_ai/events/im.py
增强了 EventBus,使其包含事件侦听器的错误处理和日志记录。
  • 添加了一个 try-except 块来捕获侦听器引发的异常。
  • 记录了侦听器执行期间发生的错误。
kirara_ai/events/event_bus.py
修改了管理器和加载器,以便在关键生命周期阶段发出事件。
  • PluginLoader 现在发出 PluginLoadedPluginStartedPluginStopped 事件。
  • LLMManager 现在发出 LLMAdapterLoadedLLMAdapterUnloaded 事件。
  • IMManager 现在发出 IMAdapterStartedIMAdapterStopped 事件。
  • 应用程序入口点现在发出 ApplicationStartedApplicationStopping 事件。
kirara_ai/plugin_manager/plugin_loader.py
kirara_ai/llm/llm_manager.py
kirara_ai/im/manager.py
kirara_ai/entry.py
重构了 PluginEventBus 以利用全局 EventBus
  • PluginEventBus 现在将 EventBus 实例作为依赖项。
  • PluginEventBus 现在向全局 EventBus 注册和取消注册侦听器。
kirara_ai/plugin_manager/plugin_event_bus.py
kirara_ai/plugin_manager/plugin_loader.py
kirara_ai/plugin_manager/plugin.py
更新了工作流执行器以发出执行事件。
  • WorkflowExecutor 现在发出 WorkflowExecutionBeginWorkflowExecutionEnd 事件。
  • WorkflowExecutor 现在将 EventBus 实例作为依赖项。
kirara_ai/workflow/core/execution/executor.py
kirara_ai/workflow/core/dispatch/dispatcher.py
@Event 装饰器已重命名为 @listen
  • @Event 装饰器已重命名为 @listen
kirara_ai/events/listen.py

提示和命令

与 Sourcery 交互

  • 触发新的审查: 在拉取请求上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审查评论。
  • 从审查评论生成 GitHub issue: 通过回复审查评论,要求 Sourcery 从审查评论创建一个 issue。您也可以回复审查评论并使用 @sourcery-ai issue 从中创建一个 issue。
  • 生成拉取请求标题: 在拉取请求标题中的任何位置写入 @sourcery-ai 以随时生成标题。您也可以在拉取请求上评论 @sourcery-ai title 以随时(重新)生成标题。
  • 生成拉取请求摘要: 在拉取请求正文中的任何位置写入 @sourcery-ai summary 以随时在您想要的位置生成 PR 摘要。您也可以在拉取请求上评论 @sourcery-ai summary 以随时(重新)生成摘要。
  • 生成审查者指南: 在拉取请求上评论 @sourcery-ai guide 以随时(重新)生成审查者指南。
  • 解决所有 Sourcery 评论: 在拉取请求上评论 @sourcery-ai resolve 以解决所有 Sourcery 评论。如果您已经解决了所有评论并且不想再看到它们,这将非常有用。
  • 驳回所有 Sourcery 审查: 在拉取请求上评论 @sourcery-ai dismiss 以驳回所有现有的 Sourcery 审查。如果您想从新的审查开始,这将特别有用 - 不要忘记评论 @sourcery-ai review 以触发新的审查!
  • 为 issue 生成行动计划: 在 issue 上评论 @sourcery-ai plan 以生成行动计划。

自定义您的体验

访问您的 仪表板 以:

  • 启用或禁用审查功能,例如 Sourcery 生成的拉取请求摘要、审查者指南等。
  • 更改审查语言。
  • 添加、删除或编辑自定义审查说明。
  • 调整其他审查设置。

获得帮助

```
Original review guide in English

Reviewer's Guide by Sourcery

This pull request introduces a basic system event mechanism to the application. It defines new event classes for application, plugin, IM, LLM, and workflow events. The EventBus is enhanced with error handling and logging. Managers and loaders are modified to emit events during key lifecycle stages. The PluginEventBus is refactored to use the global EventBus, and the workflow executor is updated to emit execution events.

Sequence diagram for Plugin Loading

sequenceDiagram
    participant PluginLoader
    participant Plugin
    participant EventBus

    PluginLoader->>Plugin: instantiate_plugin()
    activate Plugin
    PluginLoader->>Plugin: on_load()
    Plugin->>EventBus: post(PluginLoaded(plugin))
    deactivate Plugin
Loading

Sequence diagram for Plugin Starting

sequenceDiagram
    participant PluginLoader
    participant Plugin
    participant EventBus

    PluginLoader->>Plugin: on_start()
    activate Plugin
    Plugin->>EventBus: post(PluginStarted(plugin))
    deactivate Plugin
Loading

Sequence diagram for Plugin Stopping

sequenceDiagram
    participant PluginLoader
    participant Plugin
    participant EventBus

    PluginLoader->>Plugin: on_stop()
    activate Plugin
    Plugin->>Plugin: event_bus.unregister_all()
    Plugin->>EventBus: post(PluginStopped(plugin))
    deactivate Plugin
Loading

Sequence diagram for Workflow Execution

sequenceDiagram
    participant WorkflowExecutor
    participant EventBus

    WorkflowExecutor->>EventBus: post(WorkflowExecutionBegin(workflow, self))
    activate WorkflowExecutor
    WorkflowExecutor->>WorkflowExecutor: _execute_nodes()
    WorkflowExecutor->>EventBus: post(WorkflowExecutionEnd(workflow, self, self.results))
    deactivate WorkflowExecutor
Loading

Sequence diagram for LLM Adapter Loading

sequenceDiagram
    participant LLMManager
    participant LLMBackendAdapter
    participant EventBus

    LLMManager->>LLMBackendAdapter: load_backend()
    activate LLMBackendAdapter
    LLMManager->>EventBus: post(LLMAdapterLoaded(adapter))
    deactivate LLMBackendAdapter
Loading

Sequence diagram for LLM Adapter Unloading

sequenceDiagram
    participant LLMManager
    participant LLMBackendAdapter
    participant EventBus

    LLMManager->>LLMBackendAdapter: unload_backend()
    activate LLMBackendAdapter
    LLMManager->>EventBus: post(LLMAdapterUnloaded(adapter))
    deactivate LLMBackendAdapter
Loading

Sequence diagram for IM Adapter Starting

sequenceDiagram
    participant IMManager
    participant IMAdapter
    participant EventBus

    IMManager->>IMAdapter: start()
    activate IMAdapter
    IMManager->>EventBus: post(IMAdapterStarted(adapter))
    deactivate IMAdapter
Loading

Sequence diagram for IM Adapter Stopping

sequenceDiagram
    participant IMManager
    participant IMAdapter
    participant EventBus

    IMManager->>IMAdapter: stop()
    activate IMAdapter
    IMManager->>EventBus: post(IMAdapterStopped(adapter))
    deactivate IMAdapter
Loading

Sequence diagram for Application Starting

sequenceDiagram
    participant Application
    participant EventBus

    Application->>EventBus: post(ApplicationStarted())
Loading

Sequence diagram for Application Stopping

sequenceDiagram
    participant Application
    participant EventBus

    Application->>EventBus: post(ApplicationStopping())
Loading

Updated class diagram for PluginEventBus

classDiagram
    class PluginEventBus {
        -event_bus: EventBus
        -registered_listeners: List[Callable]
        +__init__(event_bus: EventBus)
        +register(event_type: Type, listener: Callable)
        +unregister(event_type: Type, listener: Callable)
        +post(event)
        +unregister_all()
    }
    PluginEventBus -- EventBus : uses
Loading

File-Level Changes

Change Details Files
Introduced new event classes for various application components.
  • Added ApplicationStarted and ApplicationStopping events.
  • Added PluginLoaded, PluginStarted, and PluginStopped events.
  • Added IMAdapterStarted and IMAdapterStopped events.
  • Added LLMAdapterLoaded and LLMAdapterUnloaded events.
  • Added WorkflowExecutionBegin and WorkflowExecutionEnd events.
kirara_ai/events/__init__.py
kirara_ai/events/application.py
kirara_ai/events/workflow.py
kirara_ai/events/plugin.py
kirara_ai/events/llm.py
kirara_ai/events/im.py
Enhanced the EventBus to include error handling and logging for event listeners.
  • Added a try-except block to catch exceptions raised by listeners.
  • Logged errors that occur during listener execution.
kirara_ai/events/event_bus.py
Modified managers and loaders to emit events during key lifecycle stages.
  • The PluginLoader now emits PluginLoaded, PluginStarted, and PluginStopped events.
  • The LLMManager now emits LLMAdapterLoaded and LLMAdapterUnloaded events.
  • The IMManager now emits IMAdapterStarted and IMAdapterStopped events.
  • The application entry point now emits ApplicationStarted and ApplicationStopping events.
kirara_ai/plugin_manager/plugin_loader.py
kirara_ai/llm/llm_manager.py
kirara_ai/im/manager.py
kirara_ai/entry.py
Refactored PluginEventBus to utilize the global EventBus.
  • The PluginEventBus now takes an EventBus instance as a dependency.
  • The PluginEventBus now registers and unregisters listeners with the global EventBus.
kirara_ai/plugin_manager/plugin_event_bus.py
kirara_ai/plugin_manager/plugin_loader.py
kirara_ai/plugin_manager/plugin.py
Updated the workflow executor to emit execution events.
  • The WorkflowExecutor now emits WorkflowExecutionBegin and WorkflowExecutionEnd events.
  • The WorkflowExecutor now takes an EventBus instance as a dependency.
kirara_ai/workflow/core/execution/executor.py
kirara_ai/workflow/core/dispatch/dispatcher.py
The @Event decorator was renamed to @listen.
  • The @Event decorator was renamed to @listen.
kirara_ai/events/listen.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

@lss233 - 我已经审查了你的更改 - 这里有一些反馈:

总体评论

  • 考虑为事件类添加一个基类或协议,以提供一个公共接口。
  • 这些测试现在正在注册 EventBus,但没有使用它 - 考虑添加一个断言来验证是否发布了一个事件。
以下是我在审查期间查看的内容
  • 🟡 一般问题:发现 4 个问题
  • 🟢 安全性:一切看起来都很好
  • 🟡 测试:发现 1 个问题
  • 🟢 复杂性:一切看起来都很好
  • 🟢 文档:一切看起来都很好

Sourcery 对开源是免费的 - 如果你喜欢我们的评论,请考虑分享它们 ✨
帮助我更有用!请点击每个评论上的 👍 或 👎,我将使用反馈来改进你的评论。
Original comment in English

Hey @lss233 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a base class or protocol for the event classes to provide a common interface.
  • The tests are now registering the EventBus, but not using it - consider adding an assertion that an event was posted.
Here's what I looked at during the review
  • 🟡 General issues: 4 issues found
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link

codecov bot commented Mar 9, 2025

Codecov Report

Attention: Patch coverage is 78.68852% with 26 lines in your changes missing coverage. Please review.

Project coverage is 69.25%. Comparing base (604c9bf) to head (4c558c0).
Report is 6 commits behind head on master.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
kirara_ai/plugin_manager/plugin_event_bus.py 50.00% 6 Missing ⚠️
kirara_ai/events/event_bus.py 33.33% 4 Missing ⚠️
kirara_ai/entry.py 25.00% 3 Missing ⚠️
kirara_ai/plugin_manager/plugin_loader.py 70.00% 3 Missing ⚠️
kirara_ai/workflow/core/dispatch/dispatcher.py 0.00% 3 Missing ⚠️
kirara_ai/events/application.py 66.66% 2 Missing ⚠️
kirara_ai/events/plugin.py 83.33% 2 Missing ⚠️
kirara_ai/events/im.py 90.00% 1 Missing ⚠️
kirara_ai/events/llm.py 90.00% 1 Missing ⚠️
kirara_ai/events/workflow.py 92.85% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1428      +/-   ##
==========================================
+ Coverage   68.82%   69.25%   +0.43%     
==========================================
  Files         106      113       +7     
  Lines        4718     4820     +102     
==========================================
+ Hits         3247     3338      +91     
- Misses       1471     1482      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

- Add new event classes for application, plugin, IM, LLM, and workflow events
- Enhance EventBus with error handling and logging
- Modify managers and loaders to emit events during key lifecycle stages
- Refactor PluginEventBus to use global EventBus
- Update workflow executor to emit execution events
@lss233 lss233 force-pushed the feature/basic_events branch from 2351f97 to 3219e02 Compare March 9, 2025 17:02
- Move LLM adapter event posting outside of the loop in LLMManager
- Fix Ollama adapter model list parsing to use correct JSON key
- Update backend deletion route to use correct backend enable flag
@lss233 lss233 merged commit 1252fa2 into master Mar 9, 2025
7 checks passed
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