Content-overlay update of all GA framework files from upstream baseb4393af(2026-05-18) to1eb8c4c(2026-05-25). Applied as an overlay because GADesktop and upstream share no common git history. - 26 framework files brought to upstream: tui_v3 (new), fsapp rework, llmcore hardening, plugins/hooks lifecycle hooks, goal_mode prompt rewrite, supergrok_proxy, qq/tg/wechat/st apps, cost_tracker py3.9 compat, docs; removed obsolete install scripts. - Desktop frontend (frontends/desktop/*) and desktop_bridge.py left untouched. Upstream's only overlapping change (bridge mykey.txt-> mykey.py) is already present in our version, so it is a no-op.
67 lines
1.5 KiB
Python
67 lines
1.5 KiB
Python
import os
|
|
import sys
|
|
import importlib
|
|
|
|
# 模块级注册表: event_name -> [callback, ...]
|
|
_registry = {}
|
|
_PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
|
|
def register(event):
|
|
def decorator(fn):
|
|
_registry.setdefault(event, []).append(fn)
|
|
return fn
|
|
return decorator
|
|
|
|
|
|
def trigger(event, ctx: dict):
|
|
for fn in _registry.get(event, []):
|
|
try:
|
|
r = fn(ctx)
|
|
if isinstance(r, dict):
|
|
ctx = r
|
|
except Exception as e:
|
|
sys.stderr.write(f"[hooks] {event} callback error: {e}\n")
|
|
return ctx
|
|
|
|
|
|
def unregister(event, fn):
|
|
try:
|
|
_registry[event] = [f for f in _registry[event] if f is not fn]
|
|
except KeyError:
|
|
pass
|
|
|
|
|
|
def clear(event=None):
|
|
if event:
|
|
_registry.pop(event, None)
|
|
else:
|
|
_registry.clear()
|
|
|
|
|
|
def has(event):
|
|
return bool(_registry.get(event))
|
|
|
|
|
|
def discover_and_load(plugin_dir=None):
|
|
if plugin_dir is None:
|
|
plugin_dir = os.path.join(_PROJECT_ROOT, 'plugins')
|
|
if not os.path.isdir(plugin_dir):
|
|
return
|
|
parent = os.path.dirname(plugin_dir)
|
|
if parent not in sys.path:
|
|
sys.path.insert(0, parent)
|
|
for fn in sorted(os.listdir(plugin_dir)):
|
|
if fn.startswith('_') or not fn.endswith('.py'):
|
|
continue
|
|
name = fn[:-3]
|
|
load(name)
|
|
|
|
|
|
def load(name):
|
|
try:
|
|
importlib.import_module(f'plugins.{name}')
|
|
return True
|
|
except Exception as e:
|
|
sys.stderr.write(f"[hooks] plugin '{name}' load failed: {e}\n")
|
|
return False |