Skip to content

Commit

Permalink
refactor: 配置文件均改为json
Browse files Browse the repository at this point in the history
  • Loading branch information
RockChinQ committed Feb 6, 2024
1 parent f340a44 commit c853bba
Show file tree
Hide file tree
Showing 48 changed files with 357 additions and 287 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ bard.json
!/docker-compose.yaml
res/instance_id.json
.DS_Store
/data
3 changes: 1 addition & 2 deletions pkg/audit/center/groups/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def __init__(self, prefix: str, ap: app.Application):
super().__init__(prefix+"/main", ap)

async def do(self, *args, **kwargs):
config = self.ap.cfg_mgr.data
if not config['report_usage']:
if not self.ap.system_cfg.data['report-usage']:
return None
return await super().do(*args, **kwargs)

Expand Down
3 changes: 1 addition & 2 deletions pkg/audit/center/groups/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def __init__(self, prefix: str, ap: app.Application):
super().__init__(prefix+"/plugin", ap)

async def do(self, *args, **kwargs):
config = self.ap.cfg_mgr.data
if not config['report_usage']:
if not self.ap.system_cfg.data['report-usage']:
return None
return await super().do(*args, **kwargs)

Expand Down
3 changes: 1 addition & 2 deletions pkg/audit/center/groups/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def __init__(self, prefix: str, ap: app.Application):
super().__init__(prefix+"/usage", ap)

async def do(self, *args, **kwargs):
config = self.ap.cfg_mgr.data
if not config['report_usage']:
if not self.ap.system_cfg.data['report-usage']:
return None
return await super().do(*args, **kwargs)

Expand Down
8 changes: 5 additions & 3 deletions pkg/command/cmdmgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ..provider import entities as llm_entities
from . import entities, operator, errors

from .operators import func, plugin, default, reset, list as list_cmd, last, next, delc, resend, prompt, cfg, cmd, help, version, update
from .operators import func, plugin, default, reset, list as list_cmd, last, next, delc, resend, prompt, cmd, help, version, update


class CommandManager:
Expand Down Expand Up @@ -85,9 +85,11 @@ async def execute(
"""

privilege = 1
if query.sender_id == self.ap.cfg_mgr.data['admin_qq'] \
or query.sender_id in self.ap.cfg_mgr['admin_qq']:

if f'{query.launcher_type.value}_{query.launcher_id}' in self.ap.system_cfg.data['admin-sessions']:
privilege = 2

print(f'privilege: {privilege}')

ctx = entities.ExecuteContext(
query=query,
Expand Down
1 change: 1 addition & 0 deletions pkg/command/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def decorator(cls: typing.Type[CommandOperator]) -> typing.Type[CommandOperator]
cls.help = help
cls.usage = usage
cls.parent_class = parent_class
cls.lowest_privilege = privilege

preregistered_operators.append(cls)

Expand Down
98 changes: 0 additions & 98 deletions pkg/command/operators/cfg.py

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/command/operators/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async def execute(
self,
context: entities.ExecuteContext
) -> typing.AsyncGenerator[entities.CommandReturn, None]:
help = self.ap.tips_mgr.data['help_message']
help = self.ap.system_cfg.data['help-message']

help += '\n发送命令 !cmd 可查看命令列表'

Expand Down
3 changes: 3 additions & 0 deletions pkg/config/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from .impls import pymodule, json as json_file


managers: ConfigManager = []


class ConfigManager:
"""配置文件管理器"""

Expand Down
10 changes: 8 additions & 2 deletions pkg/core/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ class Application:

tool_mgr: llm_tool_mgr.ToolManager = None

cfg_mgr: config_mgr.ConfigManager = None
command_cfg: config_mgr.ConfigManager = None

tips_mgr: config_mgr.ConfigManager = None
pipeline_cfg: config_mgr.ConfigManager = None

platform_cfg: config_mgr.ConfigManager = None

provider_cfg: config_mgr.ConfigManager = None

system_cfg: config_mgr.ConfigManager = None

ctr_mgr: center_mgr.V2CenterAPI = None

Expand Down
40 changes: 16 additions & 24 deletions pkg/core/boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def make_app() -> app.Application:
generated_files = await files.generate_files()

if generated_files:
print("以下文件不存在,已自动生成,请修改配置文件后重启:")
print("以下文件不存在,已自动生成,请按需修改配置文件后重启:")
for file in generated_files:
print("-", file)

Expand All @@ -52,31 +52,23 @@ async def make_app() -> app.Application:
# 生成标识符
identifier.init()

cfg_mgr = await config.load_python_module_config("config.py", "config-template.py")
cfg = cfg_mgr.data
# ========== 加载配置文件 ==========

# 检查是否携带了 --override 或 -r 参数
if "--override" in sys.argv or "-r" in sys.argv:
use_override = True
command_cfg = await config.load_json_config("data/config/command.json", "templates/command.json")
pipeline_cfg = await config.load_json_config("data/config/pipeline.json", "templates/pipeline.json")
platform_cfg = await config.load_json_config("data/config/platform.json", "templates/platform.json")
provider_cfg = await config.load_json_config("data/config/provider.json", "templates/provider.json")
system_cfg = await config.load_json_config("data/config/system.json", "templates/system.json")

if use_override:
overrided = await config.override_config_manager(cfg_mgr)
if overrided:
qcg_logger.info("以下配置项已使用 override.json 覆盖:" + ",".join(overrided))

tips_mgr = await config.load_python_module_config(
"tips.py", "tips-custom-template.py"
)

# 检查管理员QQ号
if cfg_mgr.data["admin_qq"] == 0:
qcg_logger.warning("未设置管理员QQ号,将无法使用管理员命令,请在 config.py 中修改 admin_qq")

# 构建组建实例
# ========== 构建应用实例 ==========
ap = app.Application()
ap.logger = qcg_logger
ap.cfg_mgr = cfg_mgr
ap.tips_mgr = tips_mgr

ap.command_cfg = command_cfg
ap.pipeline_cfg = pipeline_cfg
ap.platform_cfg = platform_cfg
ap.provider_cfg = provider_cfg
ap.system_cfg = system_cfg

proxy_mgr = proxy.ProxyManager(ap)
await proxy_mgr.initialize()
Expand All @@ -95,8 +87,8 @@ async def make_app() -> app.Application:
"platform": sys.platform,
},
runtime_info={
"admin_id": "{}".format(cfg["admin_qq"]),
"msg_source": cfg["msg_source_adapter"],
"admin_id": "{}".format(system_cfg.data["admin-sessions"]),
"msg_source": platform_cfg.data["platform-adapter"],
},
)
ap.ctr_mgr = center_v2_api
Expand Down
24 changes: 15 additions & 9 deletions pkg/core/bootutils/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@


required_files = {
"config.py": "config-template.py",
"banlist.py": "banlist-template.py",
"tips.py": "tips-custom-template.py",
"sensitive.json": "res/templates/sensitive-template.json",
"scenario/default.json": "scenario/default-template.json",
"cmdpriv.json": "res/templates/cmdpriv-template.json",
"plugins/__init__.py": "templates/__init__.py",
"plugins/plugins.json": "templates/plugin-settings.json",
"data/config/command.json": "templates/command.json",
"data/config/pipeline.json": "templates/pipeline.json",
"data/config/platform.json": "templates/platform.json",
"data/config/provider.json": "templates/provider.json",
"data/config/system.json": "templates/system.json",
"data/config/sensitive-words.json": "templates/sensitive-words.json",
"data/scenario/default.json": "templates/scenario-template.json",
}

required_paths = [
"plugins",
"prompts",
"temp",
"logs"
"data",
"data/prompts",
"data/scenario",
"data/logs",
"data/config",
"plugins"
]

async def generate_files() -> list[str]:
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/bootutils/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def init_logging() -> logging.Logger:
if "DEBUG" in os.environ and os.environ["DEBUG"] in ["true", "1"]:
level = logging.DEBUG

log_file_name = "logs/qcg-%s.log" % time.strftime(
log_file_name = "data/logs/qcg-%s.log" % time.strftime(
"%Y-%m-%d-%H-%M-%S", time.localtime()
)

Expand Down
10 changes: 4 additions & 6 deletions pkg/core/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from ..pipeline import entities as pipeline_entities
from ..plugin import events

DEFAULT_QUERY_CONCURRENCY = 10


class Controller:
"""总控制器
Expand All @@ -21,7 +19,7 @@ class Controller:

def __init__(self, ap: app.Application):
self.ap = ap
self.semaphore = asyncio.Semaphore(DEFAULT_QUERY_CONCURRENCY)
self.semaphore = asyncio.Semaphore(self.ap.system_cfg.data['pipeline-concurrency'])

async def consumer(self):
"""事件处理循环
Expand Down Expand Up @@ -150,9 +148,9 @@ async def process_query(self, query: entities.Query):
try:
await self._execute_from_stage(0, query)
except Exception as e:
self.ap.logger.error(f"处理请求时出错 {query}: {e}")
# self.ap.logger.debug(f"处理请求时出错 {query}: {e}", exc_info=True)
traceback.print_exc()
self.ap.logger.error(f"处理请求时出错 query_id={query.query_id}: {e}")
self.ap.logger.debug(f"Traceback: {traceback.format_exc()}")
# traceback.print_exc()
finally:
self.ap.logger.debug(f"Query {query} processed")

Expand Down
Loading

0 comments on commit c853bba

Please sign in to comment.