selftest / selftest (push) Canceled after 0s
Wire deliver_report/listen_pack/retry hooks; gen --auto-retry and intro light check; make AGENTS_MIN the up-to-date agent entry with full command map. Co-authored-by: Cursor <cursoragent@cursor.com>
97 lines
3.0 KiB
Python
97 lines
3.0 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""薄钩子总线:内核不知道插件细节,只按名字广播。
|
||
|
||
钩子约定(均可选):
|
||
before_build_tags kwargs: args, cfg, style, chill, instrumental, lean
|
||
after_assemble kwargs: tags, lyrics, args, cfg, style, lean
|
||
-> 可返回 {"tags":..., "lyrics":...} 覆盖
|
||
after_gen kwargs: run_dir, manifest, cfg, args
|
||
after_qc kwargs: run_dir, report, verd, rows, cfg, args
|
||
before_qc_judge kwargs: result, expect, soft (qc_worker 子进程内,可选)
|
||
after_qc_judge kwargs: verdict, why, result
|
||
|
||
插件目录:
|
||
plugins/builtin/*.py 内置,默认加载
|
||
plugins/<name>/plugin.py 第三方包式
|
||
plugins/<name>.py 单文件(非 hooks/__init__)
|
||
"""
|
||
import importlib
|
||
import os
|
||
import pkgutil
|
||
import sys
|
||
import traceback
|
||
|
||
_HERE = os.path.dirname(os.path.abspath(__file__))
|
||
_LOADED = False
|
||
|
||
|
||
class HookBus(object):
|
||
def __init__(self):
|
||
self._handlers = {}
|
||
|
||
def on(self, name):
|
||
def deco(fn):
|
||
self._handlers.setdefault(name, []).append(fn)
|
||
return fn
|
||
return deco
|
||
|
||
def emit(self, name, **kwargs):
|
||
last = None
|
||
for fn in self._handlers.get(name, []):
|
||
try:
|
||
ret = fn(**kwargs)
|
||
if ret is not None:
|
||
last = ret
|
||
except Exception:
|
||
traceback.print_exc()
|
||
return last
|
||
|
||
|
||
bus = HookBus()
|
||
|
||
|
||
def load_plugins(extra_dirs=None):
|
||
"""发现并 import 插件(幂等)。"""
|
||
global _LOADED
|
||
if _LOADED:
|
||
return
|
||
_LOADED = True
|
||
roots = [_HERE]
|
||
if extra_dirs:
|
||
roots.extend(extra_dirs)
|
||
# 保证 songforge.plugins 可被 import
|
||
parent = os.path.dirname(_HERE)
|
||
if parent not in sys.path:
|
||
sys.path.insert(0, parent)
|
||
# builtin
|
||
builtin = os.path.join(_HERE, "builtin")
|
||
if os.path.isdir(builtin):
|
||
for info in pkgutil.iter_modules([builtin]):
|
||
if info.name.startswith("_"):
|
||
continue
|
||
try:
|
||
importlib.import_module(
|
||
"songforge.plugins.builtin.%s" % info.name)
|
||
except Exception:
|
||
traceback.print_exc()
|
||
# 顶层单文件 / 子包 plugin.py
|
||
for name in sorted(os.listdir(_HERE)):
|
||
if name.startswith("_") or name in ("builtin", "hooks.py",
|
||
"__init__.py", "README.md"):
|
||
continue
|
||
path = os.path.join(_HERE, name)
|
||
try:
|
||
if name.endswith(".py"):
|
||
mod = "songforge.plugins.%s" % name[:-3]
|
||
importlib.import_module(mod)
|
||
elif os.path.isdir(path) and os.path.isfile(
|
||
os.path.join(path, "plugin.py")):
|
||
importlib.import_module(
|
||
"songforge.plugins.%s.plugin" % name)
|
||
except Exception:
|
||
traceback.print_exc()
|
||
|
||
|
||
# 首次 import hooks 时加载内置插件
|
||
load_plugins()
|