mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-09 03:30:48 +08:00
重构仓库目录为统一的 runtime 分层并清理历史 openclaw 残留。
本次迁移将网关/通道/工具/技能/脚本与协议资源集中到新结构,统一路径常量与脚本转发机制,减少顶层噪音并保证运行与测试行为一致。 Made-with: Cursor
This commit is contained in:
parent
ba3836f00f
commit
4a23b715a2
498 changed files with 2760 additions and 2200 deletions
75
runtime/hooks/frontmatter.py
Normal file
75
runtime/hooks/frontmatter.py
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Dict, Optional, Tuple
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ParsedHookFrontmatter:
|
||||
frontmatter: Dict[str, Any]
|
||||
body: str
|
||||
|
||||
|
||||
def parse_frontmatter(markdown: str) -> ParsedHookFrontmatter:
|
||||
"""
|
||||
Parse YAML frontmatter delimited by leading '---' lines.
|
||||
Returns (frontmatter_dict, remaining_body).
|
||||
"""
|
||||
text = markdown.lstrip("\ufeff")
|
||||
if not text.startswith("---"):
|
||||
return ParsedHookFrontmatter(frontmatter={}, body=markdown)
|
||||
|
||||
lines = text.splitlines(keepends=True)
|
||||
if not lines:
|
||||
return ParsedHookFrontmatter(frontmatter={}, body=markdown)
|
||||
|
||||
# first line is '---'
|
||||
if not lines[0].strip().startswith("---"):
|
||||
return ParsedHookFrontmatter(frontmatter={}, body=markdown)
|
||||
|
||||
fm_lines = []
|
||||
end_idx = None
|
||||
for i in range(1, len(lines)):
|
||||
if lines[i].strip().startswith("---"):
|
||||
end_idx = i
|
||||
break
|
||||
fm_lines.append(lines[i])
|
||||
|
||||
if end_idx is None:
|
||||
return ParsedHookFrontmatter(frontmatter={}, body=markdown)
|
||||
|
||||
fm_raw = "".join(fm_lines)
|
||||
body = "".join(lines[end_idx + 1 :])
|
||||
|
||||
try:
|
||||
parsed = yaml.safe_load(fm_raw) or {}
|
||||
if not isinstance(parsed, dict):
|
||||
parsed = {}
|
||||
except Exception:
|
||||
parsed = {}
|
||||
return ParsedHookFrontmatter(frontmatter=parsed, body=body)
|
||||
|
||||
|
||||
def resolve_oclaw_metadata(frontmatter: Dict[str, Any]) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Oclaw embeds metadata as a JSON-like object under key 'metadata' -> 'oclaw'
|
||||
(or sometimes as already-parsed dict). We normalize to a dict if present.
|
||||
"""
|
||||
meta = frontmatter.get("metadata")
|
||||
if not isinstance(meta, dict):
|
||||
return None
|
||||
oc = meta.get("oclaw")
|
||||
return oc if isinstance(oc, dict) else None
|
||||
|
||||
|
||||
def resolve_hook_key(name: str, entry: Dict[str, Any]) -> str:
|
||||
# Oclaw's TS resolves a config key with possible explicit hookKey; keep it simple.
|
||||
md = entry.get("metadata") if isinstance(entry, dict) else None
|
||||
if isinstance(md, dict):
|
||||
hook_key = md.get("hookKey")
|
||||
if isinstance(hook_key, str) and hook_key.strip():
|
||||
return hook_key.strip()
|
||||
return name
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue