|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""SessionStart 宿主适配器:读取事件与本机插件配置,输出项目记忆。""" |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import contextlib |
| 6 | +import json |
| 7 | +import os |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +PLUGIN_ROOT = next((c for c in Path(__file__).resolve().parents if (c / "plugin.json").is_file()), Path(__file__).resolve().parents[1]) |
| 13 | +sys.path.insert(0, str(PLUGIN_ROOT / "scripts")) |
| 14 | + |
| 15 | +from codeguard import startup_application |
| 16 | +from codeguard.reporting import plugin_version |
| 17 | +from detect_lang import ( # 兼容原有 Python 导入接口 |
| 18 | + LANG_COMMANDS, |
| 19 | + REGISTRY, |
| 20 | + detect_languages, |
| 21 | + ensure_user_path, |
| 22 | + find_project_root, |
| 23 | + load_user_config, |
| 24 | +) |
| 25 | + |
| 26 | +__all__ = [ |
| 27 | + "LANG_COMMANDS", |
| 28 | + "REGISTRY", |
| 29 | + "detect_languages", |
| 30 | + "detect_linter_config", |
| 31 | + "ensure_user_path", |
| 32 | + "find_project_root", |
| 33 | + "load_user_config", |
| 34 | + "main", |
| 35 | + "notify", |
| 36 | + "version_backlog_note", |
| 37 | +] |
| 38 | + |
| 39 | +detect_linter_config = startup_application.detect_linter_config |
| 40 | +version_backlog_note = startup_application.version_backlog_note |
| 41 | + |
| 42 | + |
| 43 | +def notify(title: str, message: str) -> None: |
| 44 | + """macOS 系统通知;其他宿主静默。""" |
| 45 | + if sys.platform != "darwin": |
| 46 | + return |
| 47 | + safe_title = title.replace('"', "'") |
| 48 | + safe_message = message.replace('"', "'")[:200] |
| 49 | + with contextlib.suppress(OSError): |
| 50 | + subprocess.Popen( |
| 51 | + ["osascript", "-e", |
| 52 | + f'display notification "{safe_message}" with title "{safe_title}"'], |
| 53 | + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def _enabled_plugin_orgs() -> tuple[str, ...]: |
| 58 | + """ZCode 宿主注册表只在适配器读取,其他宿主缺文件时无副作用。""" |
| 59 | + try: |
| 60 | + config = json.loads((Path.home() / ".zcode" / "cli" / "config.json").read_text(encoding="utf-8")) |
| 61 | + orgs = { |
| 62 | + key.split("@", 1)[1] |
| 63 | + for key in (config.get("plugins", {}) or {}).get("enabledPlugins", {}) |
| 64 | + if key.startswith("codeguard@") |
| 65 | + } |
| 66 | + return tuple(sorted(orgs)) |
| 67 | + except (OSError, ValueError, AttributeError): |
| 68 | + return () |
| 69 | + |
| 70 | + |
| 71 | +def main(payload: dict | None = None) -> int: |
| 72 | + from codeguard.hook_state import observe_once, session_scope |
| 73 | + session_id = (payload or {}).get("session_id") |
| 74 | + with session_scope(payload or {}): |
| 75 | + return observe_once(f"start:{session_id}" if session_id else None, lambda: _main(payload)) |
| 76 | + |
| 77 | + |
| 78 | +def _main(payload: dict | None = None) -> int: |
| 79 | + ensure_user_path(from_login_shell=True) |
| 80 | + project_root = find_project_root(os.getcwd()) or Path(os.getcwd()) |
| 81 | + current = plugin_version() |
| 82 | + report = startup_application.build_startup_report( |
| 83 | + project_root, |
| 84 | + plugin_version="" if current == "unknown" else current, |
| 85 | + cache_root=Path.home() / ".zcode" / "cli" / "plugins" / "cache", |
| 86 | + enabled_plugin_orgs=_enabled_plugin_orgs(), |
| 87 | + ) |
| 88 | + if report.notification: |
| 89 | + notify(*report.notification) |
| 90 | + if report.text: |
| 91 | + print(report.text) |
| 92 | + return 0 |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + try: |
| 97 | + payload: dict = {} |
| 98 | + if not sys.stdin.isatty(): |
| 99 | + try: |
| 100 | + data = json.loads(sys.stdin.read()) |
| 101 | + if isinstance(data, dict): |
| 102 | + payload = data |
| 103 | + except (json.JSONDecodeError, ValueError): |
| 104 | + payload = {} |
| 105 | + sys.exit(main(payload)) |
| 106 | + except SystemExit: |
| 107 | + raise |
| 108 | + except Exception as exc: # noqa: BLE001 — 内部错误 fail-open,不阻断宿主工作流 |
| 109 | + print(f"[codeguard] 内部错误已忽略(fail-open): {exc!r}", file=sys.stderr) |
| 110 | + sys.exit(0) |
0 commit comments