mirror of
https://github.com/langbot-app/LangBot.git
synced 2025-11-25 19:37:36 +08:00
feat: 添加启动信息阶段
This commit is contained in:
@@ -15,7 +15,7 @@ from ..command import cmdmgr
|
|||||||
from ..plugin import manager as plugin_mgr
|
from ..plugin import manager as plugin_mgr
|
||||||
from ..pipeline import pool
|
from ..pipeline import pool
|
||||||
from ..pipeline import controller, stagemgr
|
from ..pipeline import controller, stagemgr
|
||||||
from ..utils import version as version_mgr, proxy as proxy_mgr
|
from ..utils import version as version_mgr, proxy as proxy_mgr, announce as announce_mgr
|
||||||
|
|
||||||
|
|
||||||
class Application:
|
class Application:
|
||||||
@@ -69,6 +69,8 @@ class Application:
|
|||||||
|
|
||||||
ver_mgr: version_mgr.VersionManager = None
|
ver_mgr: version_mgr.VersionManager = None
|
||||||
|
|
||||||
|
ann_mgr: announce_mgr.AnnouncementManager = None
|
||||||
|
|
||||||
proxy_mgr: proxy_mgr.ProxyManager = None
|
proxy_mgr: proxy_mgr.ProxyManager = None
|
||||||
|
|
||||||
logger: logging.Logger = None
|
logger: logging.Logger = None
|
||||||
|
|||||||
@@ -7,14 +7,15 @@ from ..audit import identifier
|
|||||||
from . import stage
|
from . import stage
|
||||||
|
|
||||||
# 引入启动阶段实现以便注册
|
# 引入启动阶段实现以便注册
|
||||||
from .stages import load_config, setup_logger, build_app, migrate
|
from .stages import load_config, setup_logger, build_app, migrate, show_notes
|
||||||
|
|
||||||
|
|
||||||
stage_order = [
|
stage_order = [
|
||||||
"LoadConfigStage",
|
"LoadConfigStage",
|
||||||
"MigrationStage",
|
"MigrationStage",
|
||||||
"SetupLoggerStage",
|
"SetupLoggerStage",
|
||||||
"BuildAppStage"
|
"BuildAppStage",
|
||||||
|
"ShowNotesStage"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
44
pkg/core/note.py
Normal file
44
pkg/core/note.py
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import abc
|
||||||
|
import typing
|
||||||
|
|
||||||
|
from . import app
|
||||||
|
|
||||||
|
preregistered_notes: list[typing.Type[LaunchNote]] = []
|
||||||
|
|
||||||
|
def note_class(name: str, number: int):
|
||||||
|
"""注册一个启动信息
|
||||||
|
"""
|
||||||
|
def decorator(cls: typing.Type[LaunchNote]) -> typing.Type[LaunchNote]:
|
||||||
|
cls.name = name
|
||||||
|
cls.number = number
|
||||||
|
preregistered_notes.append(cls)
|
||||||
|
return cls
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
|
class LaunchNote(abc.ABC):
|
||||||
|
"""启动信息
|
||||||
|
"""
|
||||||
|
name: str
|
||||||
|
|
||||||
|
number: int
|
||||||
|
|
||||||
|
ap: app.Application
|
||||||
|
|
||||||
|
def __init__(self, ap: app.Application):
|
||||||
|
self.ap = ap
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
async def need_show(self) -> bool:
|
||||||
|
"""判断当前环境是否需要显示此启动信息
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
async def yield_note(self) -> typing.AsyncGenerator[typing.Tuple[str, int], None]:
|
||||||
|
"""生成启动信息
|
||||||
|
"""
|
||||||
|
pass
|
||||||
0
pkg/core/notes/__init__.py
Normal file
0
pkg/core/notes/__init__.py
Normal file
20
pkg/core/notes/n001_classic_msgs.py
Normal file
20
pkg/core/notes/n001_classic_msgs.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import typing
|
||||||
|
|
||||||
|
from .. import note, app
|
||||||
|
|
||||||
|
|
||||||
|
@note.note_class("ClassicNotes", 1)
|
||||||
|
class ClassicNotes(note.LaunchNote):
|
||||||
|
"""经典启动信息
|
||||||
|
"""
|
||||||
|
|
||||||
|
async def need_show(self) -> bool:
|
||||||
|
return True
|
||||||
|
|
||||||
|
async def yield_note(self) -> typing.AsyncGenerator[typing.Tuple[str, int], None]:
|
||||||
|
|
||||||
|
yield await self.ap.ann_mgr.show_announcements()
|
||||||
|
|
||||||
|
yield await self.ap.ver_mgr.show_version_update()
|
||||||
@@ -53,12 +53,10 @@ class BuildAppStage(stage.BootingStage):
|
|||||||
|
|
||||||
# 发送公告
|
# 发送公告
|
||||||
ann_mgr = announce.AnnouncementManager(ap)
|
ann_mgr = announce.AnnouncementManager(ap)
|
||||||
await ann_mgr.show_announcements()
|
ap.ann_mgr = ann_mgr
|
||||||
|
|
||||||
ap.query_pool = pool.QueryPool()
|
ap.query_pool = pool.QueryPool()
|
||||||
|
|
||||||
await ap.ver_mgr.show_version_update()
|
|
||||||
|
|
||||||
plugin_mgr_inst = plugin_mgr.PluginManager(ap)
|
plugin_mgr_inst = plugin_mgr.PluginManager(ap)
|
||||||
await plugin_mgr_inst.initialize()
|
await plugin_mgr_inst.initialize()
|
||||||
ap.plugin_mgr = plugin_mgr_inst
|
ap.plugin_mgr = plugin_mgr_inst
|
||||||
|
|||||||
22
pkg/core/stages/show_notes.py
Normal file
22
pkg/core/stages/show_notes.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from .. import stage, app, note
|
||||||
|
from ..notes import n001_classic_msgs
|
||||||
|
|
||||||
|
|
||||||
|
@stage.stage_class("ShowNotesStage")
|
||||||
|
class ShowNotesStage(stage.BootingStage):
|
||||||
|
"""显示启动信息阶段
|
||||||
|
"""
|
||||||
|
|
||||||
|
async def run(self, ap: app.Application):
|
||||||
|
|
||||||
|
for note_cls in note.preregistered_notes:
|
||||||
|
note_inst = note_cls(ap)
|
||||||
|
if await note_inst.need_show():
|
||||||
|
async for ret in note_inst.yield_note():
|
||||||
|
if not ret:
|
||||||
|
continue
|
||||||
|
msg, level = ret
|
||||||
|
if msg:
|
||||||
|
ap.logger.log(level, msg)
|
||||||
@@ -4,6 +4,7 @@ import json
|
|||||||
import typing
|
import typing
|
||||||
import os
|
import os
|
||||||
import base64
|
import base64
|
||||||
|
import logging
|
||||||
|
|
||||||
import pydantic
|
import pydantic
|
||||||
import requests
|
import requests
|
||||||
@@ -107,17 +108,20 @@ class AnnouncementManager:
|
|||||||
|
|
||||||
async def show_announcements(
|
async def show_announcements(
|
||||||
self
|
self
|
||||||
):
|
) -> typing.Tuple[str, int]:
|
||||||
"""显示公告"""
|
"""显示公告"""
|
||||||
try:
|
try:
|
||||||
announcements = await self.fetch_new()
|
announcements = await self.fetch_new()
|
||||||
|
ann_text = ""
|
||||||
for ann in announcements:
|
for ann in announcements:
|
||||||
self.ap.logger.info(f'[公告] {ann.time}: {ann.content}')
|
ann_text += f"[公告] {ann.time}: {ann.content}\n"
|
||||||
|
|
||||||
if announcements:
|
if announcements:
|
||||||
|
|
||||||
await self.ap.ctr_mgr.main.post_announcement_showed(
|
await self.ap.ctr_mgr.main.post_announcement_showed(
|
||||||
ids=[item.id for item in announcements]
|
ids=[item.id for item in announcements]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return ann_text, logging.INFO
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.ap.logger.warning(f'获取公告时出错: {e}')
|
return f'获取公告时出错: {e}', logging.WARNING
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import typing
|
||||||
|
import logging
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
@@ -213,11 +215,11 @@ class VersionManager:
|
|||||||
|
|
||||||
async def show_version_update(
|
async def show_version_update(
|
||||||
self
|
self
|
||||||
):
|
) -> typing.Tuple[str, int]:
|
||||||
try:
|
try:
|
||||||
|
|
||||||
if await self.ap.ver_mgr.is_new_version_available():
|
if await self.ap.ver_mgr.is_new_version_available():
|
||||||
self.ap.logger.info("有新版本可用,请使用 !update 命令更新")
|
return "有新版本可用,请使用管理员账号发送 !update 命令更新", logging.INFO
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.ap.logger.warning(f"检查版本更新时出错: {e}")
|
return f"检查版本更新时出错: {e}", logging.WARNING
|
||||||
|
|||||||
Reference in New Issue
Block a user